Something like this should work:
User.includes(:profile).where('profiles.first_name IS NOT ? AND profiles.last_name IS NOT ?', nil, nil)
Update: Replaced != with IS NOT. The operator != is not allowed in sql. It should be working now.
Question
I'm trying to select all records from a model besides the records which have nil fields in an associated record. So far I have this:
User.where("id NOT IN (?)", User.all(:include => :profile, :conditions => {:profiles => {:first_name => nil, :last_name => nil}}))
I'm wondering if there is a more direct or optimal way to do this.
Thanks!
Solution
Something like this should work:
User.includes(:profile).where('profiles.first_name IS NOT ? AND profiles.last_name IS NOT ?', nil, nil)
Update: Replaced != with IS NOT. The operator != is not allowed in sql. It should be working now.