Let a question always :include its author, how?

Posted by Freewind on Stack Overflow See other posts from Stack Overflow or by Freewind
Published on 2010-06-14T09:39:40Z Indexed on 2010/06/14 9:42 UTC
Read the original article Hit count: 396

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?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about belongs-to