views:

315

answers:

1

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!

+2  A: 

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>
Michał Kwiatkowski
thank you Michal, I was misunderstanding the way to use it. I had seen that the author had put a named_score "tagged_with" and as I said i had tried that but did not interpret it correctly. I appreciate the response. However, when I tried using<p> <%= f.label :name_like_or_description_like_or_tagged_with, "Name" %><br /> <%= f.text_field :name_like_or_description_like_or_tagged_with %> </p>I got the error "wrong number of arguments (0 for 1)" when I entered any value in the field. Do you know why it would do this?
bob
What's the code in your action that raises this error?Have you tried executing `Product.name_like_or_description_like_or_tagged_with("...")` from the Rails console?
Michał Kwiatkowski
I tried both setting up a scope_procedure and it works fine if its just as you had said in your :matching example where you aren't doing the name_or_description_or_matched_like. I can't chain the tagged_with nor a custom scope so I'm now sure what to do and I was hoping somebody had done this before. I tried both in console and in app.
bob