[Ruby] code review

Ofer Matan ofer at stanfordalumni.org
Sun Oct 29 17:32:43 PST 2006


The following code is a class method for an
ActiveRecord that gets all objects with a particular
filter, but I wanted it also to allow all the other
fancy find options that ActiveRecord provides:

  MASTER_ID_IS_NULL = " and master_id is NULL"
  def self.masters(options={})
    default_options = {:conditions => 'true'}
    options = default_options.merge(options)
    if options[:conditions].is_a?(Array) then
      options[:conditions][0] += MASTER_ID_IS_NULL
    else #string
      options[:conditions] += MASTER_ID_IS_NULL
    end
    find(:all,options)
  end   

examples of use:

myobject.masters 
myobject.masters(:conditions=>"id<5",:limit=>5)
myobject.masters(:conditions=>["name is like
%?%",name])

This works fine but I am looking for comments from
more experienced rubyists. Is there a more ruby-ish
way to do this?

-Ofer


More information about the Ruby mailing list