ruby-on-rails - Ruby:水龙头不工作?

标签 ruby-on-rails ruby class tap

为我的 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/

相关文章:

ruby-on-rails - 登录在 rails 内外都使用的 gem

ruby-on-rails - 分割YouTube网址 rails

c++ - C++获取类的所有对象

c++ - 默认构造函数参数

ruby - 如何阻止 Ruby 自动添加回车符

c++ - 使用 .push_back 将新创建的项目添加到库存中

ruby-on-rails - Rails STI Controller

MySQL - 如何选择具有唯一名称的行?

ruby-on-rails - 显示与所选产品所在类别相同的随机产品图像。ROR 应用程序

Ruby Gem 包管理器因 Gem::GemNotFoundException 失败