2015-05-27 5 views
7

मेरे पास कक्षा Node है कि मैंने acts_as_taggable सेटअप किया है, इसलिए मैं किसी भी नोड में user_tags जोड़ सकता हूं। मेरे पास Node मॉडल पर एक विधि भी है जो user_tag_list के सभी उपयोगकर्ताओं के लिए वास्तविक User रिकॉर्ड देखेंगे।मैं किसी ऑब्जेक्ट की आभासी विशेषता के साथ pg_search के साथ कैसे खोज करूं?

[32] pry(main)> m = Node.find(85) 
    Node Load (8.6ms) SELECT "nodes".* FROM "nodes" WHERE "nodes"."id" = $1 LIMIT 1 [["id", 85]] 
=> #<Node id: 85, name: "House Fire 2", family_tree_id: 57, user_id: 57, media_id: 228, media_type: "Video", created_at: "2015-05-15 00:20:26", updated_at: "2015-05-20 01:06:34"> 
[33] pry(main)> m.user_tags 
    ActsAsTaggableOn::Tag Load (3.8ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND "taggings"."context" = 'user_tags' [["taggable_id", 85], ["taggable_type", "Node"]] 
=> [#<ActsAsTaggableOn::Tag id: 4, name: "[email protected]", taggings_count: 1>, #<ActsAsTaggableOn::Tag id: 6, name: "[email protected]", taggings_count: 1>] 
[34] pry(main)> m.user_tag_list 
    ActsAsTaggableOn::Tag Load (0.8ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'user_tags' AND taggings.tagger_id IS NULL) [["taggable_id", 85], ["taggable_type", "Node"]] 
=> ["[email protected]", "[email protected]"] 
[35] pry(main)> m.tagged_users 
    User Load (5.7ms) SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 
    User Load (2.1ms) SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 
=> [#<User id: 52, email: "[email protected]", encrypted_password: "$2a$10$KaX1kvtIw1.jGITnt9Czqeq3xTzhY3OM052NSHsL5Lf...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 5, current_sign_in_at: "2015-04-03 17:10:28", last_sign_in_at: "2015-04-03 00:38:24", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2015-03-05 01:36:31", updated_at: "2015-04-03 17:10:28", first_name: "Gerry ", confirmation_token: nil, confirmed_at: "2015-03-05 01:36:52", confirmation_sent_at: nil, unconfirmed_email: nil, invitation_relation: "uncle", avatar: nil, invitation_token: nil, invitation_created_at: "2015-03-05 01:36:31", invitation_sent_at: "2015-03-05 01:36:31", invitation_accepted_at: "2015-03-05 01:36:52", invitation_limit: nil, invited_by_id: 1, invited_by_type: "User", invitations_count: 0, bio: nil, last_name: "Atrick", gender: 0>, 
#<User id: 58, email: "[email protected]", encrypted_password: "$2a$10$ZpzLH17iFrOXzH4U/pOX.e4nwN.9IJ1s1Ap/zQglk9K...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 6, current_sign_in_at: "2015-05-26 04:36:32", last_sign_in_at: "2015-04-03 00:14:55", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2015-03-12 03:39:28", updated_at: "2015-05-26 04:36:32", first_name: "Daniel", confirmation_token: nil, confirmed_at: "2015-03-12 05:46:18", confirmation_sent_at: nil, unconfirmed_email: nil, invitation_relation: "son", avatar: nil, invitation_token: nil, invitation_created_at: "2015-03-12 03:39:28", invitation_sent_at: "2015-03-12 03:39:28", invitation_accepted_at: "2015-03-12 05:46:18", invitation_limit: nil, invited_by_id: 57, invited_by_type: "User", invitations_count: 0, bio: nil, last_name: "Marty", gender: 0>] 

तो, आदर्श, मुझे क्या करना चाहते हैं उन कहना Gerry के लिए खोज करने के लिए है और यह ऊपर के उदाहरण से Node.id = 85 वापसी होगी अनुमति देते हैं, क्योंकि Node.id = 85 पहला नाम के साथ एक उपयोगकर्ता टैग है: यहाँ एक उदाहरण है Gerry

यह मेरा मौजूदा pg_searchNode पर:

pg_search_scope :node_search, against: [:name, :user_id, :circa], 
    using: { tsearch: { any_word: true} }, 
    :associated_against => { 
     comments: [:message], 
     user: [:first_name, :last_name, :email], 
     memberships: [:relation] 
    } 

मुझे लगता है कि मैं pg_search की गतिशील गुंजाइश उपयोग करने में सक्षम होना चाहिए, लेकिन मैं काफी यह grok नहीं कर सकते हैं।

मैं इसे कैसे प्राप्त करूं?

उत्तर

0

यह पता चला है कि उत्तर काफी सीधे है।

यह देखते हुए कि यह मेरा Node मॉडल में acts-as-taggable-on की घोषणा है:

include PgSearch 
    pg_search_scope :node_search, against: [:name, :user_id, :circa], 
    using: { tsearch: { any_word: true, dictionary: :english} }, 
    :associated_against => { 
     comments: [:message], 
     user: [:first_name, :last_name, :email], 
     memberships: [:relation], 
     user_tags: [:name] 
    } 

ध्यान दें कि मैं था:

acts_as_taggable_on :user_tags 

समाधान तो जैसे pg_search घोषणा में इस स्तंभ जोड़ने के लिए बस है user_tags (यानी :name विशेषता) के भीतर फ़ील्ड निर्दिष्ट करने के लिए।

तो अब, यह :user_tags की उस वर्चुअल विशेषता के विरुद्ध खोज करता है जैसा मैं चाहता था।

संबंधित मुद्दे