Rails 3.0 One-One关联在WHERE子句中使用关联模型

时间:2011-02-24 18:40:13

标签: ruby-on-rails-3

当我这样做时:

conditions = {:first_name => 'Chris'}
Patient.joins(:user).find(:all, :conditions => conditions)

它生成(并且因为first_name不在Patient表中而失败)

SELECT "patients".* FROM "patients" INNER JOIN "users" ON "users"."id" = "patients"."user_id" WHERE "patients"."first_name" = 'Chris'

我还需要能够查询用户模型的字段并找回Patient对象。这可能吗?

2 个答案:

答案 0 :(得分:0)

尝试将条件哈希更改为:

conditions = {'users.first_name' => 'Chris'}

我在Rails 2.3中使用过这种风格,它对我很有用。干杯!

答案 1 :(得分:0)

试试这个:

conditions = ['users.first_name = ?', 'Chris']
Patient.joins(:user).find(:all, :conditions => conditions)