简体   繁体   中英

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:

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. 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}%"] }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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