开发者

How to: Searchlogic and Tags

开发者 https://www.devze.com 2022-12-24 09:09 出处:网络
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.

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!


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>
0

精彩评论

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