如何:搜索逻辑和标签

发布于 2024-08-25 13:26:36 字数 825 浏览 4 评论 0原文

我已经安装了 searchlogic 并添加了 will_paginate 等。

我目前有一个产品模型,它使用acts_as_taggable_on 插件启用了标记。我想使用 searchlogic 搜索标签。

这是可标记插件页面: http://github.com/mbleigh/acts-as-taggable-on

每个产品都有一个“tag_list”,我可以使用 Product.tag_list 访问它

,或者我可以使用 Product.tags[0] 访问特定标签,

但我无法通过搜索逻辑找到用于搜索的范围。这是我的工作表格的一部分。

<p>
    <%= f.label :name_or_description_like, "Name" %><br />
    <%= f.text_field :name_or_description_like %>
</p>

我已经尝试过 :name_or_description_or_tagged_with_like 和 :name_or_description_or_tags_like 以及 :name_or_description_or_tags_list_like 尝试让它工作,但我一直有一个错误,说找不到我尝试过的选项(未找到命名范围)。我想知道如何才能实现此功能,或者如何创建自己的named_scope,它允许我搜索由可标记插件添加到每个产品的标签。

谢谢!

I have installed searchlogic and added will_paginate etc.

I currently have a product model that has tagging enabled using the acts_as_taggable_on plugin. I want to search the tags using searchlogic.

Here is the taggable plugin page:
http://github.com/mbleigh/acts-as-taggable-on

Each product has a "tag_list" that i can access using Product.tag_list

or i can access a specific tag using Product.tags[0]

I can't find the scope to use for searching however with search logic. Here is my part of my working form.

<p>
    <%= f.label :name_or_description_like, "Name" %><br />
    <%= f.text_field :name_or_description_like %>
</p>

I have tried :name_or_description_or_tagged_with_like and :name_or_description_or_tags_like and also :name_or_description_or_tags_list_like to try and get it to work but I keep have an error that says the options i have tried are not found (named scopes not found). I am wondering how I can get this working or how to create my own named_scope that would allow me to search the tags added to each product by the taggable plugin.

Thanks!

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

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

发布评论

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

评论(1

尘世孤行 2024-09-01 13:26:36

Searchlogic 使用现有的命名范围。从acts-as-taggable-on文档中我看到可标记模型获得tagged_with命名范围。因此 Product.tagged_with("tag") 应该为您提供所有标有“tag”的产品。您可以将搜索逻辑中的条件与“或”结合起来,因此如果您想查找名称类似、描述类似或标签与给定文本匹配的所有产品,您应该使用以下范围:

Product.name_like_or_description_like_or_tagged_with("...")

您可以使用 :name_like_or_description_like_or_tagged_with 直接在您的表单中,或者您可以创建自己的自定义搜索范围过程:

scope_procedure :matching, lambda {|text|
  name_like_or_description_like_or_tagged_with(text)
}

然后,在您的表单中,只需使用 :matching

<p>
    <%= f.label :matching, "Search term" %><br />
    <%= f.text_field :matching %>
</p>

Searchlogic uses existing named scopes. From the acts-as-taggable-on documentation I see that taggable models get tagged_with named scope. So Product.tagged_with("tag") should give you all products tagged with "tag". You can combine conditions in searchlogic with "or", so if you want to find all products with name like, description like or tags matching given text, you should use the following scope:

Product.name_like_or_description_like_or_tagged_with("...")

You can either use :name_like_or_description_like_or_tagged_with directly in your form, or you can create your own custom scope procedure for searching:

scope_procedure :matching, lambda {|text|
  name_like_or_description_like_or_tagged_with(text)
}

Then, in your form, just use :matching.

<p>
    <%= f.label :matching, "Search term" %><br />
    <%= f.text_field :matching %>
</p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文