I could not find an activerecord equivalent of "Not Like". I was able to find a where.not
, but that will check if a string does not match a value, as so:
User.where.not(name: 'Gabe')
is the same as:
User.where('name != ?', 'Gabe')
I was looking for a NOT LIKE, where the value is not contained in the string. The equivalent sql query would look like as follows:
SELECT * FROM users WHERE name NOT LIKE '%Gabe%'
In ActiveRecord I can currently get away with the following:
User.where("name NOT LIKE ?", "%Gabe%")
But that leaves a lot to be desired. Any new additions to Rails 4 to facilitate 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 useilike
for sqlite &like
for other platforms).You could also implement this as a scope to contain the implementation to the model: