News for hackers. Stuff that matters.
Re:Efficient GraphQL Queries in Ruby on Rails and Postgres by blueplanet200 on Wednesday September 25, 2019 @03:49PM (#21074573)
It's more composable with multiple includes.

For instance, if you have a simple active record type:

class User has_many :comments; has_many :likes; end

User.includes(:comments, :likes).find(user_id)

Generates 3 queries, but importantly the number of records returned is appropriate.

User.joins(:comments, :likes).where(users: { id: user_id })

Generates only a single query, but the number of records returned from this join is the product of comments*likes for this particular user.

I find the includes stuff easier to reason about as the amount of associations you need loaded in memory becomes more complex.

Re:Efficient GraphQL Queries in Ruby on Rails and Postgres by e12e on Thursday September 26, 2019 @11:54AM (#21082193)
Hm, I see now where we're talking past each other - I was thinking more along the lines of "user has orders has order lines has products has prices" (where you might traditionally make a (hierarchy of) database view(s)).

Not "user has orders", and also "user has favorite products".

Re:Efficient GraphQL Queries in Ruby on Rails and Postgres by e12e on Wednesday September 25, 2019 @04:08PM (#21074753)
So the problem is active record is no good at joins?

(That is, it's hard/impossible via the active record DSL to make the correct nested join?)

By the way (you may know this) when staying within active record convention, you should never need to single out the ".id" and ".foreign_model_id" fields:

User.joins(:comments, :likes).where(user: some_user)

(where some_user is an instance of User).

I'm still not quite comfortable enough with AR, but I think, leaning on this: https://www.learneroo.com/modules/137/nodes/768

I think what you want in your second case is simply?:

User.joins(:comments, :likes).where(user: some_user).uniq

(I'd have to create some models and watch what ".to_sql" creates to be certain, though)

Re:Efficient GraphQL Queries in Ruby on Rails and Postgres by imtringued on Thursday September 26, 2019 @06:58AM (#21079799)
> So the problem is active record is no good at joins?

You encounter this problem whether you use an ORM or not.