我有以下型號:幫助在Rails中加入3
class Event < ActiveRecord::Base
has_many :action_items
end
class ActionItem < ActiveRecord::Base
belongs_to :event
belongs_to :action_item_type
end
class ActionItemType < ActiveRecord::Base
has_many :action_items
end
而我想做的是,對於一個給定的事件,發現有一個動作項目類型與名稱的所有操作項目「foo」(例如)。因此,我認爲SQL會去是這樣的:
SELECT * FROM action_items a
INNER JOIN action_item_types t
ON a.action_item_type_id = t.id
WHERE a.event_id = 1
AND t.name = "foo"
任何人可以幫我翻譯成一個漂亮的活動記錄查詢呢? (Rails 3 - Arel)
謝謝!
如果你想讓它看起來更漂亮,你可以做`e.action_items.joins(:action_item_type).where(:action_item_types => {:name =>「foo」})` – Marc 2012-03-27 17:00:29