Is there a way to invert an ActiveRecord::Relation query?
Let's say we have the following: irb> Post.where(:hidden => true).to_sql => "SELECT `posts`.* FROM `posts` WHERE posts.hidden = 1" Could we somehow get an inverted SQL query out of it? What I am looking for, should probably look like this: irb> Post.where(:hidden => true).invert.to_sql => "SELECT `posts`.* FROM `posts` WHERE NOT (posts.hidden = 1)" With a different syntax, yes. Example: posts = Post.scoped.table # or Arel::Table.new("posts") posts.where(posts[:hidden].eq(true).not).to_sql # => SELECT FROM `posts` WHERE NOT ((`posts`.`hidden` = 1)) In rails 4 there is not suffix for this