Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 鲁比:水龙头坏了?_Ruby On Rails_Ruby_Class_Tap - Fatal编程技术网

Ruby on rails 鲁比:水龙头坏了?

Ruby on rails 鲁比:水龙头坏了?,ruby-on-rails,ruby,class,tap,Ruby On Rails,Ruby,Class,Tap,为我的应用程序_控制器编写代码,将用户输入转换为查询,其工作原理如下: 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  # 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
对块的返回值没有任何作用,因此对没有明显副作用的块使用
tap
没有任何意义。

以下是您(实际上)想要的:

这并不是真正需要点击
tap
,这对于在不更改方法返回值的情况下操作方法中的项最有用(sepp2k已经解释了原因)


此外,最好将此查询移动到模型内部的方法中。

我想知道这是否与以下事实有关:这不是一个真正的方法链,而是Arel DSL。在任何情况下,我确信有一个Arel结构可以用来代替tap。您想完成什么?您想让tap在这里做什么?目标是有条件地调用方法链中的model.where()。我准备总是调用where,并有条件地重写一个类似“1=1”的过滤器,但这有点。。。。好吧,你知道。成功!!非常感谢。关于将其移动到模型(可能是mixin)中的有趣建议,因为我所做的是从jqGrid接收请求参数并动态返回正确的结果(过滤/排序/分页)。我正在为几个型号做这项工作。是的,我看到我期望从tap得到一些不合理的东西。谢谢
result_set = model.
  includes(:metric_template => [:group]).  #still need to abstact this
  order(sort_string).
  limit(rows).
  offset((page-1)*rows)
result_set = result_set.where(f) if f