Class Methods And Scopes
So today at work we were wondering if ActiveRecord scopes behave like class methods. You may have heard that scopes are just syntactical sugar for class methods, in other words that this:
Could be written also as like this:
And yes the behaviour will be the same….mostly! Why you might not know is that between scopes and class methods they are subtle differences.
Check the following class methods, one just returns modern cabs and another returns cabs driven by an specific person
If we used them together Cab.by_driver("Mayra").modern
, the following query will be generated:
Everything works as aspected right? Lets keep working and see what would happen if we continue to use class methods. Imagine that we use the former class methods in a controller, using params
to set the driver name, and somehow a nil
value is passed:
Well this is where things get start to get messy, the following query will result like this:
This will return an array of cabs with no driver instead of cabs from all drivers. So whats the big deal you’ll say? Just add a guard validation inside by_driver
method
Again:
And we get an error :( :
So what happened? Here is what: by_driver
will return nil
because driver
is not present, so when we call modern
on nil
we get the error. So ok we can still fix this with a couple of extra code:
If driver is not present, we’ll return the all
chainable relation, and finally when we do this
We are going to get what we actually intended in the beginning:
Even if is working now, all this could have actually been easily resolved with scopes:
Because scopes always return chainables objects ;)