On Jul 16, 11:42 am, Jermaine <jermaine2...@gmail.com> wrote:
> Hi everyone,
>
> I have a working named_scope here, however it's too cluttered.
> Anyone got a better (more efficient) and concise alternative?
>
> Thanks.
>

well named_scope :filter, lambda {|*args|
  {:conditions  => {:status => args.first, :invoice_number =>
args.second}.reject{|k,v| v.blank?}}
}

is more compact and i think does the same thing as your code. With
anything like this any time you save when building up the conditions
will be dwarfed by the time it takes to run the actual query

Fred



> ============================================
> named_scope :filter, lambda { |*args|
>          if
>            args.first && args.second == nil
>              {} # return all records
>
>            if args.first.blank?
>                { :conditions => ["status = ?", args.second] }
>
>            if
>              args.second.blank?
>                { :conditions => ["invoice_number= ?", args.first] }
>
>          else
>            { :conditions => ["nvoice_number= ? and status = ?",
> args.first, args.second] }
>        end
>      end
>    end
>  }

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to