尝试使用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数组。
我哪里做错了?
因为列id
存在于两个表foos
和users
中,所以DB不知道你在where语句中引用了哪一个,尝试:
where(foos: {id: something})
要么
where(users: {id: something})