Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.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/9/ruby-on-rails-3/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 Rails:如何使用;加入;在sql查询中,一个对象有一个关联_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails Rails:如何使用;加入;在sql查询中,一个对象有一个关联

Ruby on rails Rails:如何使用;加入;在sql查询中,一个对象有一个关联,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我有一个用户模型和一个配置文件模型。每个用户都有一个配置文件。配置文件具有城市属性。现在,我希望Users控制器的index操作能够拉入与当前用户所在城市相同的所有用户。我在控制器中有以下代码: def index @users = User.same_city_as(current_user).paginate :page => params[:page], :per_page => 10 end 我还有一个用户模型: scope :same_city_as, lambda {

我有一个用户模型和一个配置文件模型。每个用户都有一个配置文件。配置文件具有城市属性。现在,我希望Users控制器的index操作能够拉入与当前用户所在城市相同的所有用户。我在控制器中有以下代码:

def index @users = User.same_city_as(current_user).paginate :page => params[:page], :per_page => 10 end
我还有一个用户模型:

scope :same_city_as, lambda { |user| joins(:profile).where(:profile => {:city => user.profile.city} ) }
这是行不通的。我收到此错误“PGError:error:表“profile”的子句条目中缺少。”

请尝试使用:

…where(user.profile=>{city:user.profile.city})

您需要在where子句中设置
:profile

 joins(:profile).where(:profiles => {:city => user.profile.city})