私はRailsの自己参照クラスについて多くのことを読んだことがありますが、引き続き問題を起こしています。自己参照has_many through in Rails
私は記事のクラスを持っていて、ソースの記事から結果の記事まで、互いに参照できるようにして、その逆を見つけることができるようにします。ですから、私はhas_manyを通して、Linksという別のクラスを使ってやろうとしています。
私のスキーマが
create_table "articles", :force => true do |t|
t.string "name"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "links", :force => true do |t|
t.integer "source_id"
t.integer "outcome_id"
t.string "question"
t.datetime "created_at"
t.datetime "updated_at"
end
モデルは
class Article < ActiveRecord::Base
has_many :links_as_source, :foreign_key => "source_id", :class_name => "Link"
has_many :sources, :through => :links_as_source
has_many :links_as_outcome, :foreign_key => "outcome_id", :class_name => "Link"
has_many :outcomes, :through => :links_as_outcome
end
と
class Link < ActiveRecord::Base
belongs_to :source, :foreign_key => "source_id", :class_name => "Article"
belongs_to :outcome, :foreign_key => "outcome_id", :class_name => "Article"
end
ているされて私は、コンソールで記事を作成することができ、私はa.outcomes << b
を使用して、一緒に記事をリンクすることができますが、リンクテーブルはsource_idではなくoutcome_idのみを格納しています。
私は間違っていますか?
a.outcomes << B'あなたは 'a.save'を呼び出している'呼び出した後? –
私はこれを最後に働かせました。私は名前を変更しました - それが重要かどうかわかりません。私はどこかでソースが何かのために使う馬鹿な名前だったと読んだ。 これはうまくいきます: create_table "article_relationships"、:force => true do | t | t.integer "PARENT_ID" t.integerは ... エンド CREATE_TABLE "記事"、 "child_id":力=>真DOを| T | t.string "name" ... end – Edward