This post tells you how to using the thinking sphinx rails plugin to search a bunch of records that have a specific tag (acts_as_taggable_on).
class Photo < ActiveRecord::Base ... acts_as_taggable_on :keywords define_index do indexes :caption indexes keywords.name, :as => :keywords end ... end
Now lets assign some tags to our Photo
>> photo = Photo.create :user_id => 1, :caption => 'BMW M7', :keyword_list => 'BMW, Car' >> photo.keywords => [#<Tag id: 1, name: "BMW">, #<Tag id: 3, name: "Car">]
Lets run the indexer
# rake thinking_sphinx:indexNow lets test it...
>> Photo.search 'Car' => [#<Photo id: 100, caption: "BMW M7", user_id: 1, publication_state: "Unpublished", created_at: "2009-11-20 00:00:47", updated_at: "2009-11-20 00:00:47", image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil>]
>> Photo.search('Car').first.keywords => [#<Tag id: 1, name: "BMW">, #<Tag id: 3, name: "Car">]
Search by tags only
>> Photo.search :conditions => {:keywords => 'Car'} => [#<Photo id: 100, caption: "BMW M7", user_id: 1, publication_state: "Unpublished", created_at: "2009-11-20 00:00:47", updated_at: "2009-11-20 00:00:47", image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil>]
Comments
Thx for tips :)
Thx for tips :)
Post new comment