为我的 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)
这不起作用,因为似乎没有调用 where 方法:
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() 在这里工作。为什么不呢?它不能作为类方法使用吗?能令人信服吗?
感谢任何指导。
最佳答案
where
被调用就好了。问题是 where
没有任何可见的副作用 - 它仅用于返回值。
由于 tap
不会对 block 的返回值执行任何操作,因此将 tap
与没有明显副作用的 block 一起使用是没有意义的。
关于ruby-on-rails - Ruby:水龙头不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11405235/