6

के लिए एसोसिएशन में मेरी स्कीमा Articles और Journals है जिसे Tags के साथ टैग किया जा सकता है। इसके लिए तालिका में शामिल होने के लिए एक बहुलक संबंध के साथ has_many through: एसोसिएशन की आवश्यकता है।दो पॉलीमोर्फिक के साथ रेल मॉडल है_मनी: ऑब्जेक्ट टैगिंग

ठीक है, यह आसान और अच्छी तरह से प्रलेखित हिस्सा है।

मेरी समस्या यह है कि Articles प्राथमिक-टैग और उप-टैग दोनों हो सकते हैं। प्राथमिक टैग वे हैं जो मुझे सबसे ज्यादा रूचि रखते हैं, लेकिन मेरे मॉडल को इन सब टैग का ट्रैक रखने की भी आवश्यकता है। उप टैग केवल Article का वर्णन करने वाले लेबल हैं जो कम महत्व वाले हैं, लेकिन Tags के उसी वैश्विक पूल से आते हैं। (वास्तव में, एक Article का प्राथमिक टैग एक और उप-टैग हो सकता है)।

इस हासिल करने Tagging मॉडल के लिए दो संगठनों और Tags करने के लिए दो has_many through: संघों (यानी #tags & # उप टैग)

के लिए यह क्या मैं अब तक है, जो जबकि मान्य करता है Article मॉडल की आवश्यकता है प्राथमिक टैग और उप-टैग अलग नहीं रखें।

class Article < ActiveRecord::Base 
    has_many :taggings, as: :taggable 
    has_many :tags, through: :taggings 

    has_many :sub_taggings, as: :taggable, class_name: 'Tagging', 
      source_type: 'article_sub' 
    has_many :sub_tags, through: :sub_taggings, class_name: 'Tag', source: :tag 
end 

class Tagging < ActiveRecord::Base 
    # id   :integer 
    # taggable_id :integer 
    # taggable_type :string(255) 
    # tag_id  :integer 
    belongs_to :tag 
    belongs_to :taggable, :polymorphic => true 
end 

class Tag < ActiveRecord::Base 
    has_many :taggings 
end 

मुझे पता है कि वहाँ में कहीं मैं source और source_type का सही संयोजन खोजने की जरूरत है, लेकिन मैं इस पर काम नहीं कर सकते।

पूर्णता के लिए यहां मेरा article_spec.rb है जिसका मैं परीक्षण करने के लिए उपयोग कर रहा हूं - वर्तमान में "गलत टैग" पर असफल रहा।

describe "referencing tags" do 
    before do 
    @article.tags << Tag.find_or_create_by_name("test") 
    @article.tags << Tag.find_or_create_by_name("abd") 
    @article.sub_tags << Tag.find_or_create_by_name("test2") 
    @article.sub_tags << Tag.find_or_create_by_name("abd") 
    end 

    describe "the correct tags" do 
    its(:tags) { should include Tag.find_by_name("test") } 
    its(:tags) { should include Tag.find_by_name("abd") } 
    its(:sub_tags) { should include Tag.find_by_name("abd") } 
    its(:sub_tags) { should include Tag.find_by_name("test2") } 
    end 

    describe "the incorrect tags" do 
    its(:tags) { should_not include Tag.find_by_name("test2") } 
    its(:sub_tags) { should_not include Tag.find_by_name("test") } 
    end 
end 

इसे प्राप्त करने में किसी भी मदद के लिए अग्रिम धन्यवाद। मुख्य समस्या यह है कि मैं लेखों में sub_tags एसोसिएशन के लिए उपयोग करने के लिए स्रोत_ प्रकार का रेल बताता हूं कि कैसे काम नहीं कर सकता।

उत्तर

10

हमम ... फिर से मौन ...? क्या एसओ देता है? नमस्ते...? ब्युलर्स?

डर कभी नहीं, यहाँ जवाब है:

Single Table Inheritance में देखने के बाद (नहीं जवाब है, लेकिन अन्य थोड़ा संबंधी समस्याओं के लिए एक दिलचस्प तकनीक), मैं कई संदर्भों के संबंध में एक तो सवाल भर में ठोकर खाई एक ही मॉडल पर एक बहुलक संघ के लिए। (धन्यवाद आप अपने detailed answer के लिए hakunin, +1।)

असल में हम स्पष्ट रूप से sub_taggings सहयोग के लिए Taggings तालिका में taggable_type स्तंभ की सामग्री परिभाषित करने की जरूरत है, लेकिन नहीं source या source_type, बजाय :conditions साथ साथ।

Article मॉडल अब नीचे दिखाया गया है सभी परीक्षणों से गुजरता है:

class Article < ActiveRecord::Base 
    has_many :taggings, as: :taggable 
    has_many :tags, through: :taggings, uniq: true, dependent: :destroy 

    has_many :sub_taggings, as: :taggable, class_name: 'Tagging', 
      conditions: {taggable_type: 'article_sub_tag'}, 
      dependent: :destroy 
    has_many :sub_tags, through: :sub_taggings, class_name: 'Tag', 
      source: :tag, uniq: true 
end 

अद्यतन:

यह सही Tag मॉडल उस पर टैग कार्यात्मक रिवर्स बहुरूपी संघों उत्पादन होता है। रिवर्स एसोसिएशन (यानी टैग.र्टिकल और टैग.sub_tagged_articles) परीक्षण पास करते हैं।

class Tag < ActiveRecord::Base 
    has_many :articles, through: :taggings, source: :taggable, 
      source_type: "Article" 
    has_many :sub_tagged_articles, through: :taggings, source: :taggable, 
      source_type: "Article_sub_tag", class_name: "Article" 
end 

मैं भी विस्तार किया है और सफलतापूर्वक परीक्षण किया स्कीमा अन्य मॉडलों ही टैग मॉडल और टैगिंग तालिका में शामिल होने का उपयोग करने का & sub_tagging टैगिंग अनुमति देने के लिए। उम्मीद है कि यह किसी की मदद करता है।

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