jackc / surus

PostgreSQL extensions for ActiveRecord
MIT License
394 stars 35 forks source link

INNER JOIN renames all fields to t0_r0 ... t2_r12 to the json end result #37

Open onnimonni opened 6 years ago

onnimonni commented 6 years ago

If I use eager_load for 3 different objects Person,Group,Organization like this:

Person.eager_load(group: organization).where({ organizations: { name: 'XXX' } }).all_json

The end result will have all of it's fields renamed to t0_r0 ... t2_r12.

jackc commented 6 years ago

You definitely don't want to use eager_load with all_json. all_json uses the SQL from the base query and that will include the renamed fields that ActiveRecord uses for eager loading. To include additional nested objects you should use the include option for all_json.

As far as your original query, the intention is unclear to me. I think ActiveRecord would treat it as load all people that are part of organization 'XXX'. It would also partially load the group and organization associations due to the where clause. I'd recommend using an explicit join instead of eager load if possible. That would probably work with Surus.