开发者

searchlogic and virtual attributes

开发者 https://www.devze.com 2022-12-29 14:10 出处:网络
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(\' \')

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:

Perso开发者_如何学Gon.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}%"] }}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号