Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我使用子弹 gem 让我知道 N+1 个查询。
我想避免偶尔添加包含。
我有一个comment属于模型的user模型
comment
user
有没有办法告诉模型随时访问评论模型以包括用户?(而不是Comment.include(:user)每次都做)
Comment.include(:user)
您可以使用default_scope:
default_scope
class Comment < ActiveRecord::Base default_scope includes(:user) end Comment.first # => the same as Comment.includes(:user).first
你应该做
class Comment < ActiveRecord::Base default_scope { includes(:user) } end