67

Rails 3.2.1

Is there a way (without squeel) to use the hash syntax of ActiveRecord to construct a != operator?

Something like Product.where(id: !params[:id])

Generates SELECT products.* FROM products WHERE id != 5

Looking for the opposite of Product.where(id: params[:id])

UPDATE

In rails 4 there is a not operator.

Product.where.not(id: params[:id])

4 Answers 4

93

You can use the following

Product.where('id != ?', params[:id])

Which will generate what you are looking for, while parameterizing the query.

With Rails 4, the following syntax has been added to support not clauses

Product.where.not(id: params[:id])

Add multiple clauses with chaining...

Product.where.not(id: params[:id]).where.not(category_id: params[:cat_id])
2
  • 2
    This may be work unexcepted when params[:id] is nil. In rails 3.2.X I use Product.where('id != ?', params[:id].to_i)
    – comme
    Oct 29, 2013 at 6:41
  • In rails 3, please advise, if there are more than one id, then how to do it? ex. Product.where('id != ?', 10000, 10001, 10004, 10035, 10088)
    – rhunal
    Jan 2, 2018 at 7:33
34

There isn't any built-in way to do this (as of Rails 3.2.13). However, you can easily build a method to help you out:

ActiveRecord::Base.class_eval do
  def self.where_not(opts)
    params = []        
    sql = opts.map{|k, v| params << v; "#{quoted_table_name}.#{quote_column_name k} != ?"}.join(' AND ')
    where(sql, *params)
  end
end

And then you can do:

Product.where_not(id: params[:id])

UPDATE

As @DanMclain answered - this is already done for you in Rails 4 (using where.not(...)).

3
  • 1
    PLUS ONE TO BE SURE.... But be sure to test for nulls too.. They are (to my mind) not equal, but the resulting query will NOT return records where the field has null. where_not(:status => 'active') for instance..
    – baash05
    Mar 8, 2013 at 1:14
  • Also to be considered... where_not_or Employee.where(:id=>1).where_not_or(:job_title=>'Awesome', :wages=>10000000).update_all(:job_title=>'Awesome', :wages=>10000000, :updated_at => Time.now) would only update if things changed
    – baash05
    Mar 8, 2013 at 1:27
  • 1
    @daveatflow, SQL standard for NULLs is to never return unless specifically checked using IS NULL or IS NOT NULL. So I disagree that calling != should modify this behavior. Adding an or method would be greatly beneficial, but at this point using Arel or Squeel is easier than reinventing the wheel.
    – PinnyM
    Mar 8, 2013 at 3:51
24

Rails 4 has this figured out. So maybe you could just update your rails app

Model.where.not(:id => params[:id])
2
  • 1
    Lol, upgrade Rails to improve the syntax of his AR call? I love it, wish I could go to my management and get some of our old platforms upgraded! Mar 15, 2017 at 22:27
  • 1
    @MarkKadlec Start your own company ¯_(ツ)_/¯ Jun 13, 2019 at 3:21
14

Arel could the one you might want to explore It is included in Rails 3+ I guess

Here How you do it using Arel

Product.where(Product.arel_table[:id].not_eq(params[:id]))

and

Product.where(Product.arel_table[:id].not_eq(params[:id])).to_sql 

would generate SQL Like below

SELECT `products`.* FROM `products`  WHERE (`products`.`id` != 1)

Hope this Help

2
  • How to query on multiple attributes using this syntax?? Jan 25, 2016 at 6:49
  • .where(…) can be chained, too, so this extends to searching across multiple columns. Product.where(…).where(…)
    – Dave Land
    Apr 14, 2021 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.