编写代码为我application_controller用户输入转化为查询,这个工程:红宝石:水龙头不工作?
result_set = model # some implementation of ActiveRecord::Base as a Class
.includes(:metric_template => [:group]) #still need to abstract this
.where(f)
.order(sort_string)
.limit(rows)
.offset((page-1)*rows)
这不起作用,因为在那里方法似乎不被称为:
result_set = model
.includes(:metric_template => [:group]) #still need to abstact this
.tap{|o| o.where(f) if f}
.order(sort_string)
.limit(rows)
.offset((page-1)*rows)
我真的很喜欢.tap()在这里工作。为什么不呢?它不可用作类方法吗?它能被说服吗?
欣赏任何指导。
我不知道它是否与事实不同,这不是一个真正的方法链,而是Arel DSL。无论如何,我确信你可以使用Arel构造来代替水龙头。你想达到什么目的? – 2012-07-10 01:08:45
你想在这里做点什么? – 2012-07-10 01:09:14
目的是有条件地调用方法链中的model.where()。我准备总是调用where,并有条件地覆盖像'1 = 1'这样的过滤器,但是它有一点点......呃,你知道的。 – 2012-07-10 02:41:07