only call to_a when we have to
extract owner id calculation to a method
avoid extra empty array allocation
remove extra flat_map array
no need to to_a the scope
correctly typecast keys, remove conditionals, reduce object allocations
Merge branch 'typecast'

* typecast:
  correctly typecast keys, remove conditionals, reduce object allocations
  no need to to_a the scope
  remove extra flat_map array
  avoid extra empty array allocation
  extract owner id calculation to a method
  only call to_a when we have to