views:

21

answers:

2

Is there a better way of writing this? Is it possible to do cleanly in one line?

conditions = ["category = ?", params[:category]] if params[:category]
@events = CalendarEvent.all( :conditions => conditions )
+1  A: 

Not really too much to consolidate but you don't have a lot going on so shouldn't matter.

def action
    options = {:conditions => ['category=?', params[:category]]} if params[:category]
    @events = CalendarEvent.find(:all, options)
end
Sam
I like the hash options. This will allow me to do the same thing for the other options. thx
jspooner
yep, that's the point :)
Sam
A: 
@events = CalendarEvent.all(
   :conditions => (params[:category] ? ["category = ?", params[:category]] : nil))
Shadwell