编写命名范围的更好方法?
这个命名范围工作正常。
named_scope :search, lambda {|search_txt|
{
:conditions => ["field1 like ? or field2 like ? or field3 like ?","#{search_txt}%","#{search_txt}%","#{search_txt}%"]
}
}
而不是在条件中写三次search_txt。我可以通过在条件中仅传递一次 search_txt 来处理相同的场景吗?
像
named_scope :search, lambda {|search_txt|
{
:conditions => ["field1 like ? or field2 like ? or field3 like ?","#{search_txt}%"]
}
}
This named scope is working fine.
named_scope :search, lambda {|search_txt|
{
:conditions => ["field1 like ? or field2 like ? or field3 like ?","#{search_txt}%","#{search_txt}%","#{search_txt}%"]
}
}
Instead of writing search_txt three time in conditions. Can I handle the same scenario with passing search_txt only once in conditions ?
Something like
named_scope :search, lambda {|search_txt|
{
:conditions => ["field1 like ? or field2 like ? or field3 like ?","#{search_txt}%"]
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道这是否适用于某个范围,但还有另一种编写条件的方法:
I don't know if this works in a scope, but there is another way to write conditions:
这应该有效:
}
This should work:
}