ruby-on-railsruby-on-rails-3activerecordactive-relation

Rails 3: How to get all Posts which ids are not in a given list?


To get all posts with publisher_id equals to 10, 16, or 17, I do:

Post.where(:publisher_id => [10, 16, 17])

How would I get all posts with publisher_id not equals to 10, 16, or 17 (i.e. all possible ids besides those three) ?


Solution

  • Just perform a :

    Post.where(["publisher_id NOT IN (?)", [10, 16, 17]])