Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 我们可以用连接进行快速加载吗?_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails 我们可以用连接进行快速加载吗?

Ruby on rails 我们可以用连接进行快速加载吗?,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我知道包括所有相关的模型属性,例如: @posts = Post.includes(:user) 将加载每个帖子的所有用户属性(姓名、出生日期、登录名……)的所有帖子 如果我是对的,我们不能使用select with includes来获取如下属性: @posts = Post.includes(:user).select("posts.*, user.name as user_name") 因此,对于连接,可以使用选择: posts = Post.joins(:user).select("

我知道
包括所有相关的模型属性,例如:

@posts = Post.includes(:user)
将加载每个帖子的所有用户属性(姓名、出生日期、登录名……)的所有帖子

如果我是对的,我们不能使用select with includes来获取如下属性:

@posts = Post.includes(:user).select("posts.*, user.name as user_name")
因此,对于连接,可以使用选择:

posts = Post.joins(:user).select("posts.*, user.name as user_name")
我的问题是:当我在
joins
中使用
select
时,我们能称之为
急切加载吗

若有,原因为何

posts = Post.joins(:user).select("posts.*, user.*")
相当于?:

posts = Post.includes(:user)

你不是从这篇博文中举了个例子吗?这篇博文非常清楚地解释了
连接
包含
之间的区别和相似性?如果不是这样的话,那就是巧合


我可以问一下你问题的目的吗?可能有助于为您创建一个很好的答案:)@RichPeck我添加了一些示例,以便更清楚:)感谢更新示例!老实说,我并不完全确定这两种查询类型的机制,但我添加了一个对RailsCast的引用,该引用演示了它们是如何为您工作的我之所以这样问,是因为我想在原始查询的基础上加载一些数据,最后在关联上使用了select查询,我想你可能需要知道是的,我已经读过了,但是我在理解joins方法时仍然有一个问题:)只需要知道Post.joins(:user).select(“posts.*,user.*)是否等同于Post.includes(:user)