如何重用命名范围?
您好,我的 User
模型中有一个 named_scope
,如下所示。
named_scope :by_gender, lamdba { |gender| { :conditions => { :gender => gender } } }
我想创建另外两个命名范围,它们重新使用这个范围,例如,
named_scope :male, lambda { by_gender('male') }
named_scope :female, lambda { by_gender('female') }
知道该怎么做吗?
Hi I have a named_scope
in my User
model as following.
named_scope :by_gender, lamdba { |gender| { :conditions => { :gender => gender } } }
I want to create other two named scopes which re use this one something like,
named_scope :male, lambda { by_gender('male') }
named_scope :female, lambda { by_gender('female') }
Any idea what to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以提供执行硬连线参数传递的类方法:
或者,由于您使用的named_scope非常简单,您可以删除by_gender范围并简单地使用:
第二个选项当然是有条件的,您实际上并不明确需要by_gender范围其他任何地方。
You could provide class methods that perform the hardwired argument passing:
or, as the named_scope you are using is so simple you could cut out the by_gender scope and simply use:
The second option is of course conditional on you not actually requiring the by_gender scope explicitly anywhere else.