Moh*_*ain 0 activerecord ruby-on-rails
我有两个模型的问题和答案.和一个问题has_many答案.我怎样才能找到所有没有答案的问题?
我建议添加:counter_cache到Answer模型.然后你可以在Question模型中做这样的事情
class Question < ActiveRecord::Base
has_many :answers
named_scope :unanswered, :conditions => {:answers_count => 0}
end
Run Code Online (Sandbox Code Playgroud)
该Answer模型是这样的
class Answer < ActiveRecord::Base
belongs_to :question, :counter_cache => true
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
119 次 |
| 最近记录: |