ActiveRecord.invert_where
Ever been debugging in ActiveRecord and wanted to inverse a query? You can do that without changing the whole query!
User.where(active: true)
# => "select * from users where active = 'true'"
User.where(active: true).invert_where
# => "select * from users where active != 'true'"
It also works with multiple conditions:
User.where(active: true, subscribed: false)
# => "select * from users where active = 'true' and subscribed = 'false'"
User.where(active: true, subscribed: false).invert_where
# => "select * from users where active != 'true' and subscribed != 'false'"
It works on scopes, too:
class User < ActiveRecord::Base
scope :active, -> { where(active: true) }
end
User.active.invert_where
# => "select * from users where active != 'true'"
Tweet