ruby-on-railsruby-on-rails-4activerecordrails-activerecordruby-on-rails-4.1

Multiple association joins query with NOT operator


I have the following Rails query which works great:-

@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

I'm now trying to write the NOT equivalent but am failing miserably. I thought this would work but it keeps giving me errors:-

@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")})

Also tried this and a few other variations:-

@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"])

Can anyone tell me how I put a NOT operator in a multiple association join query?


Solution

  • You can try with .not() in Rails 4:

    @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"})