我可以在 Rails 中创建*未*命名的范围吗?
我知道您可以在 Rails 中创建命名范围,它允许您指定稍后可以构建的条件:
named_scope :active, :conditions => {:active => true}
...
MyModel.active.find(...)
这通过创建一个代理对象来实现,该代理对象直到稍后才进行评估。我想知道是否可以创建动态未命名范围?
我的意思是,是否有一个方法“foo”,我可以使用它
scope = MyModel.foo(:conditions => {:target_id => 4})
,然后将 scope
作为代理对象传递,我可以执行更多 .find
或其他操作作用域调用?
I know you can create named scopes in Rails, which allow you to specify conditions which can then be built on later:
named_scope :active, :conditions => {:active => true}
...
MyModel.active.find(...)
This works by creating a proxy object which isn't evaluated until later on. What I want to know is if it's possible to create a dynamic unnamed scope?
By which I mean, is there a method 'foo' with which I can go
scope = MyModel.foo(:conditions => {:target_id => 4})
and then pass scope
around as a proxy object that I can do more .find
s or other scoped calls on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,请检查匿名范围:
Yes, check Anonymous Scopes: