Apologies for the rather confusing title, I’m finding it rather difficult to phrase this question properly.
I have a model, call it Thing that has_many :related_things, has_many :associated_dates and has_many :assignments (users assigned to things). I want to make a single query to select all associated dates from a Thing which a user is assigned to, as well as all associated dates from the related things.
At the moment, I have this which works for getting the associated dates which are directly assigned:
AssociatedDate.includes(:thing)
.includes(:thing => :assignments)
.where('assignments.user_id' => id)
However, my attempts to also load those dates that are related through :related_things are not loaded. The closest I’ve got is just including the :related_things in the query:
AssociatedDate.includes(:thing)
.includes(:thing => [:assignments, :related_things])
.where('assignments.user_id' => id)
But then I don’t know where to go from there and on its own this does not have any effect on the outcome of the query. Is the select I want possible, and how would I go about doing it?
I think you need
joins. Have a look at the documentation here.Something like
AssociatedDate.joins(:thing).joins(:thing=>:related_thing)