Search code examples
ruby-on-railssearchvirtual-attribute

Ruby on Rails - Searching virtual attributes (tags)


I've set up a Virtual Attribute tagging system as shown by Ryan Bates here: http://railscasts.com/episodes/167-more-on-virtual-attributes

The system works really well for applying tags to my models and is something I implemented into my system a couple of days ago. I then went in and followed the simple search tutorial by Ryan Bates here: http://railscasts.com/episodes/37-simple-search-form. My issue is that now my search form searches though names and I just happen to have implemented tags - it only makes sense for me to also be searching the tags. Unfortunately I'm not extremely experienced in Ruby on Rails and the only way I could even get it to kind of work was to do a bunch of loops that looped around everything and even then I couldn't figure out how to search the array I'd got (and the loops made everything very sluggish).

Does anyone know how I could do something like this?


Solution

  • I ended up using Sunspot as shown here: http://railscasts.com/episodes/278-search-with-sunspot

    With code as shown here:

    text :taggings do
            taggings.map{ |tagging| tagging.tag.name }
    end