Created
December 30, 2008 03:30
-
-
Save jqr/41500 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Untested, but it should work :) | |
class Something < ActiveRecord::Base | |
# Simpler approach available with this small Rails patch: | |
# http://rails.lighthouseapp.com/projects/8994/tickets/1773-allow-returning-nil-from-a-named_scope-lambda | |
# | |
# named_scope :with_town_like, lambda { |term| | |
# { :conditions => ['town LIKE ?', term] } unless term.blank? | |
# } | |
# | |
# named_scope :with_hobby, lambda { |term| | |
# { :conditions => ['hobby = ?', term] } unless term.blank? | |
# } | |
# | |
# named_scope :age_at_least, lambda { |term| | |
# { :conditions => ['age >= ?', term] } unless term.blank? | |
# } | |
named_scope :with_town_like, lambda { |term| | |
if !term.blank? | |
{ :conditions => ['town LIKE ?', term] } | |
else | |
{} | |
end | |
} | |
named_scope :with_hobby, lambda { |term| | |
if !term.blank? | |
{ :conditions => ['hobby = ?', term] } | |
else | |
{} | |
end | |
} | |
named_scope :age_at_least, lambda { |term| | |
if !term.blank? | |
{ :conditions => ['age >= ?', term] } | |
else | |
{} | |
end | |
} | |
def search(options = {}) | |
town_like(options[:town]). | |
with_hobby(options[:hobby]). | |
age_at_least(options[:age]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment