搜索逻辑和虚拟属性

发布于 2024-08-31 18:05:51 字数 392 浏览 4 评论 0原文

假设我有以下模型:

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

我如何使用 searchlogic 对虚拟属性进行搜索,例如:

Person.search.name_like 'foo'

当然,我可以构造一个大语句,例如:

Person.search.first_name_like_or_last_name_like_or_... 'argh'

但肯定有一种更优雅的方法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半山落雨半山空 2024-09-07 18:05:51

Searchlogic 可以与现有的命名范围结合,并且必须在虚拟属性的情况下。其中之一是:

 named_scope :name_like, lambda { |name| { :conditions => ['first_name LIKE  ? OR last_name LIKE ? OR middle_name LIKE ?', "%#{name}%","%#{name}%","%#{name}%"] }}

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}%"] }}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文