Reputation: 10077
When no parameters are passed to the following I want to be able to select all Articles.
class Article < ActiveRecord::Base
named_scope :filter_by, lambda
{
|*args| {:conditions => [(args.first || 'id') + " = ?", (args.second || '*is_anything*')]}
}
end
For example, Article.filter_by()
should be the same as Article.all
. What is the simplest way to achieve this?
Upvotes: 0
Views: 584
Reputation: 7127
How about:
named_scope :filter_by, lambda
{
|*args| {:conditions => (args.nil? || args.empty?) ? nil : [(args.first || 'id') + " = ?", (args.second || '*is_anything*')]}
}
Upvotes: 1