[Ruby] code review

Benjamin Curtis ruby at bencurtis.com
Tue Oct 31 05:45:08 PST 2006


Take a look at http://api.rubyonrails.com/classes/ActiveRecord/ 
Base.html#M000892, which describes with_scope.  This is the method I  
use for providing class methods that wrap find.

--
Building an e-commerce site with Rails?
http://www.agilewebdevelopment.com/rails-ecommerce


On Oct 29, 2006, at 5:32 PM, Ofer Matan wrote:

> 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
> _______________________________________________
> Ruby at zenspider.com
> http://www.zenspider.com/seattle.rb
> http://www.zenspider.com/mailman/listinfo/ruby



More information about the Ruby mailing list