Writing code for my application_controller to translate user input into query, this works:
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)
this does not work because the where method seems not to be called:
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)
I'd really like .tap() to work here. Why doesn't it? Is it not available as a class method? Can it be convinced?
Appreciate any guidance.
Here's what you (effectively) want:
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
This isn't really a situation that calls for tap
, which is most useful for operating on an item within a method without changing the method's return value (for reasons that sepp2k has explained).
Also, it would probably be best to move this query into a method inside the model.