使用scope_builder在Active Record模型search()方法中有条件地构建命名范围
我正在使用 Ryan Bates 优秀的 scope_builder 有条件地构建一个新的命名范围在 Active Record 模型的 search() 方法中使用。
示例文档显示您可以执行如下操作:
# in product model
def self.search(options)
scope_builder do |builder|
builder.released.visible
builder.cheap if options[:cheap]
end
end
但是,当我在模型中包含上述代码的相关版本并调用 search() 并传递一些选项时,我返回的是 ScopeBuilder::Builder 的实例而不是使用我传递的选项执行链接命名范围的结果,这是我所期望的。
我的问题是:如何获取执行构建器(选项)而不是构建器实例的结果?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来scope_builder块设计来返回scope-builder实例,因此您必须从实例中获取结果。
尝试在实例上使用 .all 来获取结果。 例如:
来自 他的测试 ,看起来这应该可行(第 47 行:“应该能够在块中建立范围”)。
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").