views:

108

answers:

1

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.

A: 

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