Quicktip: hashes in find conditions
Roy van der Meij do 09 sep 10
Have you ever made a find condition with a lot of questionmarks in it where you wanted the same input for those questionmarks?
Like this:
named_scope :search, lambda {|x| { :conditions => ["title like ? or lead like ? or content like ?", x, x, x] }}
You can actually use hashes in stead of questionmarks. So the above code could be refactored into this:
named_scope :search, lambda {|x| { :conditions => ["title like :search or lead like :search or content like :search", { :search => x}] }}
Gepost in hor | 0 reacties
Welcome to Holland On Rails
This weblog is the official Ruby techblog from the guys at Holder, a Ruby development company. Holder is also the company behind the RubyAndRails Europe Conference in Amsterdam.Recente Jobs
Bekijk alle jobs »»
Gereedschapskist
Onmisbare tools vooriedere developer!
- Ruby On Rails
Framework voor de web 2.0 developer. Eindelijk vooruitgang! - TextMate
Editor for true pro's
Typ, tab, top :-)
Nee, niet voor Win. - Made On A Mac
En nou is het over met die saaie grijze Windows bak van je!
Auteurs op deze site
Chris Obdam
'Less is more' evangelist, past dit ook dagelijks toe op zijn tandenborstel.Chiel Wester
Snelheidswonder op Ruby wielen. Leuk om mee te pair-programmen ;-)
Plaats je reactie