我想使用 where 子句进行连接查询,但出现语法错误。
在连接中使用 where 子句的正确语法是什么?
Rails docs给出了一个使用具有单个连接的散列的示例,但找不到与我的问题类似的示例。
查询:
User.joins("user_tags").joins("tags").where("tags.value <> 'single'")
错误:
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near "tags"
LINE 1: ...LECT "users".* FROM "users" user_tags tags...
最佳答案
你可以在你的用户模型中定义
has_many :user_tags
has_many :tags, through: :user_tags
然后就用
User.joins(:tags).where.not(tags: { value: 'single' })
关于ruby-on-rails - 在 Rails activerecord 中使用 where 子句和 join 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31505408/