2016-11-06 7 views

答えて

0

できました。私が最初にtag_relationshipテーブルを作成:

create table(:tag_relationship, primary_key: false) do 
    add :parent_id, references(:tags) 
    add :child_id, references(:tags) 
end 

はセットアップに今それを私が明示的にキー名を設定し、すべてのタグモデル内many_to_many関係

schema "tags" do 
    field :name, :string 
    many_to_many :parents, Notebook.Tag, [join_through: Notebook.TagRelationship, 
    join_keys: [child_id: :id, parent_id: :id]] 
    many_to_many :children, Notebook.Tag, [join_through: Notebook.TagRelationship, 
    join_keys: [parent_id: :id, child_id: :id]] 
    timestamps() 
end 

defmodule Notebook.TagRelationship do 
    use Notebook.Web, :model 

    @primary_key false 
    schema "tag_relationship" do 
    belongs_to :parent, Tag 
    belongs_to :child, Tag 
    end 

    def changeset(struct, params \\ %{}) do 
    struct 
    |> cast(params, [:parent_id, :child_id]) 
    |> validate_required([:parent_id, :child_id]) 
    end 
end 

をスキーマ&チェンジを与えます私はそれが推測しようとするかもしれないことを知らなかったので。

今私は

B、TagRelationshipのチェンジタグの

c = TagRelationships.changeset(%TagRelationship{}, 
     {parent_id: a.id, child_id: b.id}) 
Repo.insert(c) 

との関係を作成することができます

関連する問題