6

私のスキーマはTagsとタグ付けできるArticlesJournalsです。これにはhas_many through:と、Taggingのジョインテーブルとの多相関係が関連付けられている必要があります。2つの多形を持つRailsモデルhas_many through:オブジェクトタグ付けのための関連

これは簡単で文書化された部分です。

私の問題は、Articlesがプライマリタグとサブタグの両方を持つことができるということです。プライマリタグは私が最も関心を持っているものですが、私のモデルもこれらのサブタグを追跡する必要があります。サブタグは、あまり重要ではないが、Tagsの同じグローバルプールに由来するArticleを記述する単なるラベルです。 (実際には、Articleのプライマリタグは別のサブタグかもしれません)。これを達成

は、これは私が有効ではないながら、これまで持っているものであるTaggingモデルには2つの団体やTagsに2つの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 

私はどこかに私はsourcesource_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 

これを達成するための助けを前もってありがとうございます。主な問題は、Railsにsource_typeを記事のsub_tags関連付けに使用するように指示する方法を考えることができないということです。

答えて

10

うーん...再び沈黙...?それは何を与えるのですか?こんにちは...?ブエラー?

を恐れることはありません、ここでの答えがあります:

Single Table Inheritanceない答えが、他のわずかに関連する問題についての興味深い技術)に見た後、私は複数の参照に関するSOの質問に出くわし同じモデル上の多型関連に変換する。 (あなたはdetailed answer、1のためにhakuninありがとうございます。)

基本的に我々は、明示的に代わり:conditionsと、sourceまたはsource_typesub_taggingsの関連付けのためTaggings表にtaggable_type列の内容を定義しますが、ないようにする必要があります。今、以下に示す

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 

UPDATE:

これは、タグの機能逆多型の関連付けを生成する正しいTagモデルです。逆の関連付け(すなわち、Tag.articlesと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 

私も拡張され、成功したテーブルを結合する同じTagモデルとタグ付けを使用して他のモデルの& sub_taggingをタグ付けできるようにスキーマをテストしています。これが誰かを助けることを望みます。

関連する問題