문제

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top