Is it possible to negate a scope in Rails?
In Rails 4.2, you can do: scope :original, -> { … original scope definition … } scope :not_original, -> { where.not(id: original) } It’ll use a subquery.
In Rails 4.2, you can do: scope :original, -> { … original scope definition … } scope :not_original, -> { where.not(id: original) } It’ll use a subquery.
I suggest you take a look at “Named scopes are dead” The author explains there how powerful Arel is 🙂 I hope it’ll help. EDIT #1 March 2014 As some comments state, the difference is now a matter of personal taste. However, I still personally recommend to avoid exposing Arel’s scope to an upper layer … Read more
A scope is a subset of a collection. Sounds complicated? It isn’t. Imagine this: You have Users. Now, some of those Users are subscribed to your newsletter. You marked those who receive a newsletter by adding a field to the Users Database (user.subscribed_to_newsletter = true). Naturally, you sometimes want to get those Users who are … Read more