对于那些非常了解rails 和sql 的人,我正在寻找一些您可以指出我的好信息。我的查询与本节中的“加入嵌套关联”示例非常相似 - http://guides.rubyonrails.org/active_record_querying.html#using-array-hash-of-named-associations
我的模型(略)如下,
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
我想为用户(卖家)从活动中收集订单,这是我最好的解决方法。
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
这个连接应该是什么样的?
我应该把它分解成菊花链上各种模型的范围吗?
只是为了表明我在这里有一个基本的了解,我已经能够让我的第一个工作嵌套连接表运行(我对这一成就感到非常兴奋)
BuyerFeedback
belongs_to :order
def self.from_past_events
self.joins(:order => {:post => :event}).where(:order => {:post => {:event => {:date.lt => Date.today}}})
end