Search code examples
ruby-on-railsrubyruby-on-rails-3activerecordactive-relation

Exclude conditions in active relations, how?


I am seeking a way to do the opposite of :

Model.where(:name => 'julian')

Something like :

Model.where(:name => is_not('julian'))

I have found this post. But, find(:all, :conditions => {}) is now deprecated in rails 3 and moreover, I think a cleaner way to do this must exist.

Any suggestion ?


Solution

  • The simple answer is:

    Model.where('models.name != ?', 'julian')
    

    Unless, you want to get into building actual Arel conditions, in which case you would do something like:

    Model.where(Model.arel_table[:name].not_eq('julian'))