“包含"和“包含"有什么区别?和“加入"在 ActiveRecord 查询中? [英] What's the difference between "includes" and "joins" in ActiveRecord query?
本文介绍了“包含"和“包含"有什么区别?和“加入"在 ActiveRecord 查询中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ActiveRecord 查询中的includes"和joins"有什么区别?谁能用以下两个相关模型向我解释?
What's the difference between "includes" and "joins" in ActiveRecord query? Can anyone explain to me with the following two associated models?
class Car < ActiveRecord::Base
belongs_to :store
end
class Store < ActiveRecord::Base
belongs_to :owner
has_one :car
end
推荐答案
:joins
在 sql 中将表连接在一起,:includes
急切加载关联以避免 n+1问题(执行一个查询来检索记录,然后为每个加载的关联执行一个查询).
:joins
joins tables together in sql, :includes
eager loads associations to avoid the n+1 problem (where one query is executed to retrieve the record and then one per association which is loaded).
我建议您阅读Rails 指南中的部分以获取更多信息.
I suggest you read their sections in Rails Guides to get more info.
这篇关于“包含"和“包含"有什么区别?和“加入"在 ActiveRecord 查询中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文