Rails 在查找条件中使用连接模型属性 [英] Rails using a join model attribute in a condition for a find

查看:35
本文介绍了Rails 在查找条件中使用连接模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 :has_many, :through 关联来链接两个模型,用户和地点

I'm using a :has_many, :through association to link two models, User and Place

看起来像这样 -

在用户中:

  has_many :user_places
  has_many :places, :through=>:user_places

就地:

  has_many :user_places
  has_many :users, :through=>:user_places

在 User_Place 中

In User_Place

  belongs_to :user
  belongs_to :place
  belongs_to :place_status

在最后一个注意 place_status.

On that last one note the place_status.

我想编写一个查找,返回与具有特定 place_status_id 的用户关联的所有地点.

I want to write a find that returns all places associated to a user with a particular place_status_id.

Place_Status_id 在连接模型 user_place 上.

Place_Status_id is on the join model, user_place.

所以基本上我想要

User.places.where(:place_status_id=>1)

(在导轨 3 中)

但是我得到了一个错误,因为 place_status_id 不在地点模型上.

but i get an error with that because place_status_id isnt on the place model.

有什么想法吗?谢谢大家.

Any ideas? Thanks all.

推荐答案

我相信你可以这样找到

@user.places.joins(:user_places).where(:user_places => {:place_status_id => 1})

我从未使用过 Rails 3,所以如果有任何错误,我很抱歉.

I've never used Rails 3, so I'm sorry if there's any errors.

这篇关于Rails 在查找条件中使用连接模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆