开发者

ActiveRecord condition syntax question

开发者 https://www.devze.com 2023-01-12 02:31 出处:网络
Is there a better way of writing this?Is it possible to do cleanly in one line? 开发者_开发技巧conditions = [\"category = ?\", params[:category]] if params[:category]

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 )


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


@events = CalendarEvent.all(
   :conditions => (params[:category] ? ["category = ?", params[:category]] : nil))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号