搜索逻辑和虚拟属性 [英] searchlogic and virtual attributes
本文介绍了搜索逻辑和虚拟属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下模型:
Person <AR
def name
[self.first_name,self.middle_name,self.last_name].select{|n| n.present?}.join(' ')
end
end
如何使用searchlogic对虚拟属性进行搜索,例如:
How could I do a search on the virtual attribute with searchlogic, something like:
Person.search.name_like 'foo'
当然,我可以像这样构造一个大语句:
Of courese I could construct a large statement like:
Person.search.first_name_like_or_last_name_like_or_... 'argh'
但是肯定有一种更优雅的方式.
but surely there is a more elegant way.
推荐答案
Searchlogic可以与现有的命名作用域结合使用,并且必须使用虚拟属性.这样的例子之一是:
Searchlogic can be combined with existing named scopes and must be in case of virtual attributes. One such would be:
named_scope :name_like, lambda { |name| { :conditions => ['first_name LIKE ? OR last_name LIKE ? OR middle_name LIKE ?', "%#{name}%","%#{name}%","%#{name}%"] }}
这篇关于搜索逻辑和虚拟属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文