简体   繁体   English

搜索逻辑和虚拟属性

[英]searchlogic and virtual attributes

Let's say I have the following model: 假设我有以下型号:

Person <AR
 def name
  [self.first_name,self.middle_name,self.last_name].select{|n| n.present?}.join(' ')
 end
end

How could I do a search on the virtual attribute with searchlogic, something like: 如何使用searchlogic对虚拟属性进行搜索,例如:

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 can be combined with existing named scopes and must be in case of virtual attributes. Searchlogic可以与现有的命名范围结合使用,并且必须使用虚拟属性。 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}%"] }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM