型號/ host.rbRails 3中,選擇的has_many(LEFT JOIN)
class Host < ActiveRecord::Base
has_many :report, :dependent => :delete_all
end
型號/ report.rb
class Report < ActiveRecord::Base
belongs_to :host
end
現在我要選擇與自己的報告中的所有主機包括未收到任何舉報的主持人。
我試圖通過
list=Host.all(:joins => "left join `reports` on reports.host_id=host.id")
來實現這一點,但它似乎像Rails的唯一選擇從hosts
表的字段。所以我無法通過list[0].report
訪問報表對象。 另外,如果不使用原始SQL命令,我會更高興地做出選擇 - 以更像ruby的方式。
如何在只有一個選擇並且不使用SQL的情況下獲得主機和報告?
非常感謝!這正是我正在尋找的。 – gorootde 2012-01-07 17:11:25