Ruby on rails 为“has_many”关联加载关联对象的正确方法是什么?
我使用的是RubyonRails4,我想为一个有很多关联的系统加载关联对象。也就是说,我有以下模型:Ruby on rails 为“has_many”关联加载关联对象的正确方法是什么?,ruby-on-rails,ruby,ruby-on-rails-4,associations,eager-loading,Ruby On Rails,Ruby,Ruby On Rails 4,Associations,Eager Loading,我使用的是RubyonRails4,我想为一个有很多关联的系统加载关联对象。也就是说,我有以下模型: class Article < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :article end 处理这个问题的正确方法是什么?如何使用?您应该使用以下方法: Article.includes(:comments).where(title
class Article < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :article
end
处理这个问题的正确方法是什么?如何使用?您应该使用以下方法:
Article.includes(:comments).where(title: 'Sample text')
对于第一种情况,您实际上不需要急于加载,因为您只选择了一条记录,所以这不是n+1问题。您只需执行以下操作:
article = Article.find(1)
article.comments
如果您经常使用包含您的文章,您可以定义范围:
scope :with_comments, -> { includes(:comments }
简单地说:
Article.with_comments.where(title: 'Sample text')
如果您希望包含注释的关系来自另一个关联,则不会发生任何更改,您可以同时执行以下两项操作:
@current_user.articles.includes(:comments)
及
您应该使用方法,如下所示:
Article.includes(:comments).where(title: 'Sample text')
对于第一种情况,您实际上不需要急于加载,因为您只选择了一条记录,所以这不是n+1问题。您只需执行以下操作:
article = Article.find(1)
article.comments
如果您经常使用包含您的文章,您可以定义范围:
scope :with_comments, -> { includes(:comments }
简单地说:
Article.with_comments.where(title: 'Sample text')
如果您希望包含注释的关系来自另一个关联,则不会发生任何更改,您可以同时执行以下两项操作:
@current_user.articles.includes(:comments)
及
您应该使用方法,如下所示:
Article.includes(:comments).where(title: 'Sample text')
对于第一种情况,您实际上不需要急于加载,因为您只选择了一条记录,所以这不是n+1问题。您只需执行以下操作:
article = Article.find(1)
article.comments
如果您经常使用包含您的文章,您可以定义范围:
scope :with_comments, -> { includes(:comments }
简单地说:
Article.with_comments.where(title: 'Sample text')
如果您希望包含注释的关系来自另一个关联,则不会发生任何更改,您可以同时执行以下两项操作:
@current_user.articles.includes(:comments)
及
您应该使用方法,如下所示:
Article.includes(:comments).where(title: 'Sample text')
对于第一种情况,您实际上不需要急于加载,因为您只选择了一条记录,所以这不是n+1问题。您只需执行以下操作:
article = Article.find(1)
article.comments
如果您经常使用包含您的文章,您可以定义范围:
scope :with_comments, -> { includes(:comments }
简单地说:
Article.with_comments.where(title: 'Sample text')
如果您希望包含注释的关系来自另一个关联,则不会发生任何更改,您可以同时执行以下两项操作:
@current_user.articles.includes(:comments)
及
如果希望在加载文章时自动加载注释,可以在文章模型上使用默认的\u作用域
class Article < ActiveRecord::Base
has_many :comments
default_scope :include => :comments
end
如果希望在加载文章时自动加载注释,可以在文章模型上使用默认的\u作用域
class Article < ActiveRecord::Base
has_many :comments
default_scope :include => :comments
end
如果希望在加载文章时自动加载注释,可以在文章模型上使用默认的\u作用域
class Article < ActiveRecord::Base
has_many :comments
default_scope :include => :comments
end
如果希望在加载文章时自动加载注释,可以在文章模型上使用默认的\u作用域
class Article < ActiveRecord::Base
has_many :comments
default_scope :include => :comments
end
我是否应该将includes语句直接添加到has_many association?@user502052 no,因为它甚至不能完成您的工作。当我运行如下代码时,如何解决急加载问题:@current_user.articles?-注意:我更新了问题,因为我忘了添加提到的案例。@user502052我编辑了我的答案,以涵盖您的扩展问题。我是否应该将includes语句直接添加到has_many association?@user502052否,因为它甚至不能完成您的工作。以及如何解决在我以@current_user.articles这样的方式运行代码时出现的急于加载问题:@current_user.articles?-注意:我更新了问题,因为我忘了添加提到的案例。@user502052我编辑了我的答案,以涵盖您的扩展问题。我是否应该将includes语句直接添加到has_many association?@user502052否,因为它甚至不能完成您的工作。以及如何解决在我以@current_user.articles这样的方式运行代码时出现的急于加载问题:@current_user.articles?-注意:我更新了问题,因为我忘了添加提到的案例。@user502052我编辑了我的答案,以涵盖您的扩展问题。我是否应该将includes语句直接添加到has_many association?@user502052否,因为它甚至不能完成您的工作。以及如何解决在我以@current_user.articles这样的方式运行代码时出现的急于加载问题:@current_user.articles?-注意:我更新了问题,因为我忘了添加提到的案例。@user502052我编辑了我的答案以涵盖您的扩展问题。