调用调用结果的调用
使用框架,我需要 2 个 ActiveRecord 范围:
scope :tagged_with, lambda { |tag| {:conditions => [" tags like ? ", "% #{tag} %"] } }
scope :tagged_with_any, lambda { |tag_array | [HERE NEW IMPLEMENTATION] }
我希望第二个范围基于第一个范围。如果你要进行硬编码,你会做一个 2 元素数组:
lambda { | tag_array | tagged_with(tag_array[0]).tagged_with(tag_array[1]) }
它可以工作,但是我如何做它通用
lambda { | tag_array | tags.each { |t| tagged_with(t) } }
显然不能完成这项工作。
Using a framework I need 2 ActiveRecord scopes:
scope :tagged_with, lambda { |tag| {:conditions => [" tags like ? ", "% #{tag} %"] } }
scope :tagged_with_any, lambda { |tag_array | [HERE NEW IMPLEMENTATION] }
I want the second scope to be based on the first scope. If you would do it hard coded, you would do for a 2 element array:
lambda { | tag_array | tagged_with(tag_array[0]).tagged_with(tag_array[1]) }
which works, but how do I do it generic
lambda { | tag_array | tags.each { |t| tagged_with(t) } }
clearly doesn't do the job.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是可以接受的吗?
[edit] 重命名为
tagged_with_all
因为它确实是这样做的。对于tagged_with_any
,普通命名范围不实现 OR 连接;从范围“手动”连接 OR 条件是可行的,但有点混乱。请注意,您有 Arel 或 Metawhere 等库。Is this acceptable?
[edit] renamed to
tagged_with_all
since it's what it really does. For atagged_with_any
, Vanilla named scopes do not implement OR-concatenations; concatenating ORs conditions "manually" from scopes is doable but a bit messy. Note that you have libraries like Arel or Metawhere.