For those of you that know rails and sql well, I'm looking for some good information you could point me to. My query is very similar to the 'Joining Nested Associations' example in this section - http://guides.rubyonrails.org/active_record_querying.html#using-array-hash-of-named-associations
My models (abbreviated) are as follows,
User has_many :products # User is 'great-grandparent'
Product has_many :posts # Product is grandparent #1
Event has_many :posts # Event is grandparent #2
Post belongs_to :event
Post belongs_to :product
Post has_many :orders # Post is parent
Order belongs_to :post # Order is great-grandchild, grandchild, & child
I want to collect the orders from an event for a user (the seller), and here's my best crack at it.
class Order < ActiveRecord::Base
def self.collect_for_seller_and_event(user_id, event_id)
self.joins(:post => [{:product => :user }, :event]).where(:post => [{:product => {:user_id => user_id}}, {:event_id => event_id}])
end
What what should this join look like?
Should I break this up into scopes in the various models on the daisy chain?
Just to show that I have a basic understanding here, I've been able to get my first working nested join table going (I'm pretty excited about this accomplishment)
BuyerFeedback
belongs_to :order
def self.from_past_events
self.joins(:order => {:post => :event}).where(:order => {:post => {:event => {:date.lt => Date.today}}})
end