8

Is there a way to exclude the results of recordset #1 from recordset #2 in Rails 3 or 4, Activerecord?

I have tried this before with ugly code trying to remove the id's by adding them to a hash and excluding them from the query.

Must be a clean way to remove certain id's or results from a recordset?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Rubytastic
  • 15,001
  • 18
  • 87
  • 175
  • 1
    See http://stackoverflow.com/a/17560591/276959 – Mohamad Oct 01 '15 at 14:20
  • Possible duplicate of [Rails ActiveRecord: Find All Users Except Current User](http://stackoverflow.com/questions/2672744/rails-activerecord-find-all-users-except-current-user) – Mohamad Oct 01 '15 at 14:20

1 Answers1

27

You can perform a negative where like this:

Post.where.not(author: author)

Taken from here: http://guides.rubyonrails.org/active_record_querying.html#not-conditions

Zach Taylor
  • 336
  • 4
  • 8