诗歌rails之Anonymous Scopes
# config/initializers/global_named_scopes.rb
class ActiveRecord::Base
named_scope :conditions, lambda { |*args| {:conditions => args} }
end
# models/search.rb
def find_products
scope = Product.scoped({})
scope = scope.conditions "products.name LIKE ?", "%#{keywords}%" unless keywords.blank?
scope = scope.conditions "products.price >= ?", minimum_price unless minimum_price.blank?
scope = scope.conditions "products.price <= ?", maximum_price unless maximum_price.blank?
scope = scope.conditions "products.category_id = ?", category_id unless category_id.blank?
scope
end
class ActiveRecord::Base
named_scope :conditions, lambda { |*args| {:conditions => args} }
end
# models/search.rb
def find_products
scope = Product.scoped({})
scope = scope.conditions "products.name LIKE ?", "%#{keywords}%" unless keywords.blank?
scope = scope.conditions "products.price >= ?", minimum_price unless minimum_price.blank?
scope = scope.conditions "products.price <= ?", maximum_price unless maximum_price.blank?
scope = scope.conditions "products.category_id = ?", category_id unless category_id.blank?
scope
end
莫愁前路无知己,天下无人不识君。