我有一個Ticket.rb模型,和School.rb模型。得到。關聯模型的計數
票務belongs_to
學校
學校has_many
門票
我如何使用ActiveRecord找出多少張門票每個學校都有的總.count
?
我知道Ticket.where(:school_id => -insert School ID here-).count
會給我每個學校的個人計數,但我用它來填充數據到一個圖表,所以我真的需要更多像School.tickets.count
。
這可能不會弄亂我的聯想?
感謝所有..
我認爲你應該使用一個命名的範圍。這是最乾淨的。 http://stackoverflow.com/q/2900031/1251349 –