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")
Written by Sebastian Muszyński
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#