activerecord not like query

Unfortunately ActiveRecord does not have a like query builder. I agree that the raw 'NOT LIKE' leaves a lot to be desired; you could make it a scope (scope :not_like, (column, val) -> { ... }), but AR itself does not do this.


As others have pointed out ActiveRecord does not have a nice syntax for building like statements. I would suggest using Arel as it makes the query less database platform specific (will use ilike for sqlite & like for other platforms).

User.where(User.arel_table[:name].does_not_match('%Gabe%'))

You could also implement this as a scope to contain the implementation to the model:

class User < ActiveRecord::Base
  scope :not_matching, 
    -> (str) { where(arel_table[:name].does_not_match("%#{str}%")) }
end

Just addition to the answer of "where.not" of active record. "where.not" will exclude null values also. i.e. Query User.where.not(name: 'Gabe') will leave record with name 'Gabe' but it also exclude name column with NULL values. So in this scenario the solution would be

User.where.not(name: 'Gabe') .or(User.where(name: nil))


Well, you can do something like:

User.where.not("name LIKE ?", "%Gabe%")

Note: This is only available in Rails 4.