mercredi 4 mai 2016

Find all record without association with a certain field

My question is very similar to this question: Want to find records with no associated records in Rails 3

But with a twist. Let's use their example and add my problem:

class Person
   has_many :friends
end

class Friend
   belongs_to :person
   attr_accessor :type # this can be 'best' or 'acquaintance'
end

I want to get all people without 'best' friends. The normal query for most cases that I see is to get people without any friends. And that would be:

Person.includes(:friends).where( :friends => { :person_id => nil } )

But that's not what I want. Is there a way to get all people who have no 'best' friends regardless of how many other type of friends they have?

Aucun commentaire:

Enregistrer un commentaire