Rails ActiveRecord Query for Not Equal

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])

Leave a Comment