I’m using Ryan Bates’ excellent scope_builder to conditionally build a new named scope to use in a search() method of an Active Record model.
The example documentation shows that you can do something like the following:
# in product model def self.search(options) scope_builder do |builder| builder.released.visible builder.cheap if options[:cheap] end end
But, when I include the relevant version of the above code in my model and call search() with some options passed, what I get returned is an instance of ScopeBuilder::Builder and not the results of executing the chained named scope with the options I’ve passed, which is what I would expect.
My question is: How do I get the results of executing builder(options) instead of an instance of the builder?
Looks like the scope_builder block was designed to return the scope-builder instance, so you’ll have to get the results from the instance.
Try using .all on the instance to grab the results. For instance something like:
From his tests, it looks like this should work (line 47: ‘should be able to build up scope in block’).