Last Updated: February 25, 2016
·
1.785K
· muszynskis

Rails: Retrieving objects with where not condition

Problem

To retrieve all posts from category animals, we can write something like this:

Post.where(category: "animals")

What if I want to retrieve all posts except these from category animals?

Solution

Rails 3.x

Post.where(Post.arel_table[:category].not_eq("animals"))

Rails 4.x

Post.where.not(category: "animals")

Read more about arel or where.not