ActiveRecord加入查询

问题描述 投票:1回答:1

尝试使用ActiveRecord的joins界面,具体如下:

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

在这个特殊的情况下,Foo有一个belongs_to :userUserhas_many :foos的关系

在这种特殊情况下,我收到以下错误:

Column 'id' in field list is ambiguous

在这种情况下,users是一个用户ID数组。

我哪里做错了?

ruby-on-rails activerecord ruby-on-rails-5
1个回答
1
投票

因为列id存在于两个表foosusers中,所以DB不知道你在where语句中引用了哪一个,尝试:

where(foos: {id: something})

要么

where(users: {id: something})

© www.soinside.com 2019 - 2024. All rights reserved.