开发者

Let a question always :include its author, how?

class Question < ActiveRecord::Base
   belongs_to :author
end

class Author < ActiveRecord::Base
   has_many :questions
end

When I find some questions, I usually need to get their authors at the same time, so I use:

Question.find(:all, :include=>:authors)

But I don't write the ":include" part everywhere. I hope 开发者_如何转开发I can define the "include" somewhere only once, and when I find questions, the author will be automaticly loaded. Is there any way to do this?


You can use default_scope. See here for details: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002313


I prefer to use like this:

has_many :questions, :include=>:author
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜