多个关联使用NOT运算符连接查询

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

我有以下Rails查询,它很有用: -

@basic = Pay.joins(:ee_pay => :company_pay).where(:pays => {pay_line_id: current_pay_line.id, pay_sub_head_id: 1}, :company_pays => {:description => 'Salary'}).first

我现在正在尝试编写NOT等价物,但是我的失败很糟糕。我认为这会起作用,但它一直给我错误: -

@other_pay = Pay.joins(:ee_pay => :company_pay).where(:pays => {pay_line_id: current_pay_line.id, pay_sub_head_id: 1}, :company_pays => {('description != ?', "Salary")})

还尝试了这个和其他一些变化: -

@other_pay = Pay.joins(:ee_pay => :company_pay).where(:pays => {pay_line_id: current_pay_line.id, pay_sub_head_id: 1}).where(:company_pays => ['description != ?', "Salary"])

谁能告诉我如何在多关联连接查询中放置NOT运算符?

ruby-on-rails ruby-on-rails-4 activerecord rails-activerecord ruby-on-rails-4.1
1个回答
3
投票

您可以在Rails 4中尝试使用.not()

@other_pay = Pay.joins(ee_pay: :company_pay)
                .where(pays: {pay_line_id: current_pay_line.id, pay_sub_head_id: 1})
                .where.not(company_pays: {description: "Salary"})
© www.soinside.com 2019 - 2024. All rights reserved.