简体   繁体   中英

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 (ie 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)

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.

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"}}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM