ActiveRecord加入查询

时间:2018-09-04 22:52:50

标签: ruby-on-rails activerecord ruby-on-rails-5

尝试通过以下方式使用ActiveRecord的joins界面:

Foo.joins(:user).where(user_id: users).where('users.some_col IS NOT NULL')

在这种情况下,Foo具有belongs_to :user,而User具有关系has_many :foos

在这种情况下,出现以下错误:

Column 'id' in field list is ambiguous

users在这种情况下是用户ID的数组。

我哪里出错了?

1 个答案:

答案 0 :(得分:1)

由于列id同时存在于表foosusers中,因此数据库不知道where语句中指的是哪一个,请尝试:

where(foos: {id: something})

where(users: {id: something})

相关问题