Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.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
Sql Rails Activerecord查询选择性包括_Sql_Ruby On Rails_Ruby_Rails Activerecord - Fatal编程技术网

Sql Rails Activerecord查询选择性包括

Sql Rails Activerecord查询选择性包括,sql,ruby-on-rails,ruby,rails-activerecord,Sql,Ruby On Rails,Ruby,Rails Activerecord,优化大型activerecord查询时遇到问题。我需要在请求中包含一个关联的模型,但由于返回集的大小,我只希望包含两个关联的列。例如,我有: Post.includes(:user).large_set 当我在寻找类似的东西时: Post.includes(:user.name, :user.profile_pic).large_set 我需要实际使用name和profile pic属性,因此就我所知,Post.joins(:user)不是一个选项 您必须使用join来实现您想要的功能,因为

优化大型activerecord查询时遇到问题。我需要在请求中包含一个关联的模型,但由于返回集的大小,我只希望包含两个关联的列。例如,我有:

Post.includes(:user).large_set
当我在寻找类似的东西时:

Post.includes(:user.name, :user.profile_pic).large_set

我需要实际使用name和profile pic属性,因此就我所知,Post.joins(:user)不是一个选项

您必须使用join来实现您想要的功能,因为includes没有此功能。或者您也可以选择自己的包含方法:-)

选择您正在寻找的是:

Post.select("posts.*, users.name, users.profile_pic").large_set

这不是我想要的,但你说得对,我需要选择方法,所以我将接受这个答案。从技术上讲,您的呼叫包括整个用户模型,这正是我试图避免的。最小的调用是Post.select(“posts.*,users.name,users.profile_pic”)。欢迎使用大型设置。我更新了答案以匹配对您有效的内容。