Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: How do I search tags generated by Act_As_Taggable_On with PG_Search? (postgresql)

I am currently using Act_as_taggable_on for tagging and pg_search to search through my postgresql database on my Rails 3 application.

How would I search through tags generated by the act_as_taggable_on gem with pg_search? I can view the tags of a Post by saying "Post.find(1).tag_list," but there are no "tag" columns in the Post table, so when I run the

 pg_search_scope :search_by_weight, :against => {:tag_list => 'A', :title => 'B', :content => 'C'} #,:using => [:tsearch => {:prefix => true}] #:trigram, :dmetaphone]

it gives me an error because the column Post.tag_list doesn't exist in the Post table. What is it called when you can find the value through the dot connector (i.e. mission.tag_list) but when it doesn't exist in the table? I didn't know what to type. So basically, how do I pass in a non-existent column as a params?

Also, you may have noticed I commented out the

 :using => [:tsearch => {:prefix => true}] #:trigram, :dmetaphone]

above. I can't seem to find how to install extra modules for Postgresql. Where do I type CREATE EXTENSION? (using ubuntu 11.10 and postgresql 9.1.3 -> and heroku for production)

like image 401
kibaekr Avatar asked May 19 '12 17:05

kibaekr


3 Answers

A much more simple approach is just using the association and search that through pg_search which is done like this:

class Item < ActiveRecord::Base
 include PgSearch  

  pg_search_scope :full_search, 
    :against => {  
    :item_status => 'A',
    :title => 'B',
    :description => 'C'    
   },       
    :associated_against => {
    :tags => [:name]    
   }
end 

I just implemented this in one of my projects and it worked well (searched through tag names). Unfortunately I can't figure out how to weight an associated relationship since it says "tags" column not found in Items table.

like image 81
user783437 Avatar answered Nov 10 '22 15:11

user783437


This worked for me to get the tags weighted as well

    pg_search_scope :search_by_weight, 
      :against => {  
      :title => 'B',
      :content => 'C'    
    },       
    :associated_against => {
      :tags => { :name => 'A' }    
    },
    using: {tsearch: {dictionary: "english"}}
like image 37
triendeau Avatar answered Nov 10 '22 14:11

triendeau


I wrote my implementation in plpgsql a few years ago. See my blog: http://omarqureshi.net/articles/2010-12-25-tsearch2-for-rails-applications which covers how to get it working.

like image 1
Omar Qureshi Avatar answered Nov 10 '22 15:11

Omar Qureshi