2016-11-06 4 views

답변

0

나는 그것을 할 수 있었다. 내가 먼저 tag_relationship 테이블을 생성 :

create table(:tag_relationship, primary_key: false) do 
    add :parent_id, references(:tags) 
    add :child_id, references(:tags) 
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 

이제 설정을 지정 내가 명시 적으로 키 이름을 설정

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 

모든 태그 모델 내의 many_to_many 관계 나는 그것이 무엇을 추론 하려는지 알지 못했기 때문에.

지금은

b를의 TagRelationship의 변경 집합 태그 A의

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

과의 관계를 만들 수 있습니다

관련 문제