This project is archived and is in readonly mode.
Allow returning nil from a named_scope lambda
Reported by Elijah Miller | January 17th, 2009 @ 12:49 AM | in 2.x
Returning an empty hash from a named_scope lambda basically means "do nothing."
I have found this to be a good pattern for disabling a named_scope under certain conditions, most commonly when the argument to the named_scope lambda was nil.
I propose returning nil should have the same effect as returning an empty hash. They both should mean "do nothing."
# with current implementation
named_scope :with_town_like, lambda { |term|
if !term.blank?
{ :conditions => ['town LIKE ?', term] }
else
{}
end
}
# new style, with nil as acceptable return value
named_scope :with_town_like, lambda { |term|
{ :conditions => ['town LIKE ?', term] } unless term.blank?
}
Comments and changes to this ticket
-
Daniel Morrison January 17th, 2009 @ 03:39 PM
+1
Looks good, and I've definitely wanted this pattern more than once.
-
mileszs January 17th, 2009 @ 09:27 PM
+1
Straight-forward, makes a lot of sense, and I'd use it immediately.
-
kris chambers January 19th, 2009 @ 10:10 PM
+1 Looks good. I have always been wanting this for a while.
-
kris chambers January 19th, 2009 @ 10:21 PM
"I have been wanting this for a while." One day I will learn to read my comment before posting it.
-
Repository March 12th, 2009 @ 03:14 PM
- State changed from new to resolved
- Tag changed from patch to patch, verified
(from [91b98cf0a5417ce4042a0b3cd1930d5a221b737f]) Returning nil from named scope lambda is equivalent to an empty hash [#1773 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com http://github.com/rails/rails/co...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 1773 Allow returning nil from a named_scope lambda (from [91b98cf0a5417ce4042a0b3cd1930d5a221b737f]) Returni...