Panagiotis Panagi
Panagiotis Panagi

Reputation: 10077

Match anything in Rails (Activerecord) WHERE condition

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

Answers (1)

Jeff Paquette
Jeff Paquette

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

Related Questions