0

i have the following models:

class HealthCareInsurance < ActiveRecord::Base
  has_and_belongs_to_many :companies
end

class Company < ActiveRecord::Base
  has_and_belongs_to_many :health_care_insurances
end

Now, inside my controller, i need to get only those filling the condition: "id NOT IN ( my_array )". Seems easy but i just cant make it work.

Im doing the following:

@company.health_care_insurances( :conditions => [ "id NOT IN ( ? )", insurances_array])

But for some reason it gives me the exact same elements as if i change the "NOT IN" condition for an "IN" condition. Tried "where" too, but it always returns an empty array. Any ideas?

Thanks in advance

ntonnelier
  • 1,539
  • 3
  • 23
  • 49
  • 1
    IIRC in SQL you need `id NOT IN (?)` but dependent on your rails version I'd do it using active record. What version of rails are you running? – j-dexx Mar 03 '15 at 13:05
  • Hi @japed. It's rails 4. And sorry i mistaken when i wrote the code, but the results i got were actually using the code you described: NOT IN (?) – ntonnelier Mar 03 '15 at 13:14
  • What is in `insurances_array`? – j-dexx Mar 03 '15 at 13:48
  • ["276","279",..] . Basically what i get from params[:health_care_insurance_ids], that comes from here: – ntonnelier Mar 03 '15 at 13:51
  • Does the company actually have any `HealthCareInsurance` instances that aren't in those ids? – j-dexx Mar 03 '15 at 13:53

1 Answers1

0

You should be able to do this with Rails 4

@company.health_care_insurances.where.not(id: insurances_array)
j-dexx
  • 10,286
  • 3
  • 23
  • 36