我有用戶模型,我需要創建一個查詢,這樣Rails的ActiveRecord的查詢不明確的列名錯誤
SELECT DISTINCT u.id FROM users as u
LEFT JOIN lists_users as lu ON lu.user_id = u.id
WHERE u.id != 3
AND lu.list_id != 34
我嘗試
scope :include_current_user, lambda { |user, list|
joins('LEFT JOIN lists_users AS lu ON lu.user_id = id')
.where('id != :user_id AND lu.list_id != :list_id', user_id: user.id, list_id: list.id)
}
但我有錯誤
SQLite3::SQLException: ambiguous column name: id: SELECT "users".* FROM "users" LEFT JOIN lists_users AS lu ON lu.user_id = id WHERE (id != 2 AND lu.list_id != 34)
如何在ActiveRecord查詢的幫助下創建它?
你不需要指定'users.id'來避免歧義? –
我需要這個sql示例中的inlude特定用戶ID我排除了用戶id = 3並且排除了表lists_users的lu.list_id,該表具有別名lu –
如何激活記錄創建的查詢,如示例 –