Ruby on rails Rails:如何使用;加入;在sql查询中,一个对象有一个关联
我有一个用户模型和一个配置文件模型。每个用户都有一个配置文件。配置文件具有城市属性。现在,我希望Users控制器的index操作能够拉入与当前用户所在城市相同的所有用户。我在控制器中有以下代码: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 {
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})