This project is archived and is in readonly mode.

#1773 ✓resolved
Elijah Miller

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] }

# 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

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=""></a>

People watching this ticket


Referenced by