I am using Ruby on Rails 4 and I would like to eager load associated objects for a has_many
association. That is, I have the following models:
class Article < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :article
end
Since I frequently retrieve comments
when I load articles
I would like to eager load comments
each time I search for articles
, for example in these cases:
# In these cases the eager loading should happen.
Article.find(1)
Article.where(:title => 'Sample text')
@current_user.articles
What is the proper way to handle this issue? How?