What is the actual use of ActiveRecord::SpawnMethods#except ?

29 views
Skip to first unread message

Arup Rakshit

unread,
Jul 5, 2014, 3:33:14 AM7/5/14
to rubyonra...@googlegroups.com
I really don't understand the actual usecase of the *except* method
http://api.rubyonrails.org/classes/ActiveRecord/SpawnMethods.html#method-i-except

If I don't want a condition to apply in my _query_, then why should I add it?
After adding a condition, what is the point of removing it again. This how the
doco http://api.rubyonrails.org/classes/ActiveRecord/SpawnMethods.html#method-i-except is saying.

--
================
Regards,
Arup Rakshit
================
Debugging is twice as hard as writing the code in the first place. Therefore,
if you write the code as cleverly as possible, you are, by definition, not
smart enough to debug it.

--Brian Kernighan

Matt Jones

unread,
Jul 6, 2014, 11:57:01 AM7/6/14
to rubyonra...@googlegroups.com


On Saturday, 5 July 2014 02:33:14 UTC-5, Arup Rakshit wrote:
I really don't understand the actual usecase of the *except* method
http://api.rubyonrails.org/classes/ActiveRecord/SpawnMethods.html#method-i-except

If I don't want a condition to apply in my _query_, then why should I add it?
After adding a condition, what is the point of removing it again. This how the
doco http://api.rubyonrails.org/classes/ActiveRecord/SpawnMethods.html#method-i-except is saying.


`except` is used when you want to use part of a relation but not necessarily the whole thing. It's not always useful in application code, but when dealing with generic relations it can be really useful:


Here, `except` is used to remove parts of the relation that `exists?` doesn't want to use.

--Matt Jones 

Arup Rakshit

unread,
Jul 6, 2014, 2:29:15 PM7/6/14
to rubyonra...@googlegroups.com
> `except` is used when you want to use part of a relation but not
> necessarily the whole thing. It's not always useful in application code,
> but when dealing with generic relations it can be really useful:
>
> https://github.com/rails/rails/blob/44b7d6c41610bb8ddd058b3815543985f65bf7ad
> /activerecord/lib/active_record/relation/finder_methods.rb#L296
>
> Here, `except` is used to remove parts of the relation that `exists?`
> doesn't want to use.
>
> --Matt Jones

Very useful example. Does it mean, we can use it also *scope* or on *default
scope* ? Will it be a good choice although to use scope relation if it fits any
query, with some tailoring using *except* ?

Matt Jones

unread,
Jul 7, 2014, 10:46:21 AM7/7/14
to rubyonra...@googlegroups.com


On Sunday, 6 July 2014 13:29:15 UTC-5, Arup Rakshit wrote:
> `except` is used when you want to use part of a relation but not
> necessarily the whole thing. It's not always useful in application code,
> but when dealing with generic relations it can be really useful:
>
> https://github.com/rails/rails/blob/44b7d6c41610bb8ddd058b3815543985f65bf7ad
> /activerecord/lib/active_record/relation/finder_methods.rb#L296
>
> Here, `except` is used to remove parts of the relation that `exists?`
> doesn't want to use.
>
> --Matt Jones

Very useful example. Does it mean, we can use it also *scope* or on *default
scope* ? Will it be a good choice although to use scope relation if it fits any
query, with some tailoring using *except* ?


Yep. There are also some related methods; `rewhere`, `reorder`, and `unscope`. They allow somewhat finer control than `except`.

--Matt Jones 
Reply all
Reply to author
Forward
0 new messages