Ruby on rails 3 如何始终包含用于快速加载的模型
我使用bullet gem让我知道N+1查询 我想避免偶尔添加include 我有一个Ruby on rails 3 如何始终包含用于快速加载的模型,ruby-on-rails-3,activerecord,eager-loading,Ruby On Rails 3,Activerecord,Eager Loading,我使用bullet gem让我知道N+1查询 我想避免偶尔添加include 我有一个注释模型,它属于用户模型 有没有一种方法可以告诉模型,在任何时候访问评论模型时,也包括用户?(而不是做注释。包括(:user)每次)您可以使用默认范围: class Comment < ActiveRecord::Base default_scope includes(:user) end Comment.first # => the same as Comment.includes(:use
注释
模型,它属于用户
模型
有没有一种方法可以告诉模型,在任何时候访问评论模型时,也包括用户?(而不是做
注释。包括(:user)
每次)您可以使用默认范围
:
class Comment < ActiveRecord::Base
default_scope includes(:user)
end
Comment.first # => the same as Comment.includes(:user).first
class注释与Comment.includes相同(:user).first
您应该这样做
class Comment < ActiveRecord::Base
default_scope { includes(:user) }
end
class注释
谢谢,我如何使用默认范围:order=>“在描述时创建”
?请尝试默认范围包括(:user).order(“在描述时创建”)
。