Preventing N+1 queries in Rails

☆樱花仙子☆ 提交于 2019-11-28 12:50:01
fl00r

You can't use relations as Class methods. It is instance methods. You can call

@user.favorites

Check out this screencast about Eager Loading

http://railscasts.com/episodes/22-eager-loading

It will be

 User.find(:all, :include => :favorites)

or for Rails 3.x

 User.includes(:favorites)

You can add :include to your model's associations to eager load the second-order associations when the object is loaded.

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-belongs_to http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!