0
tag.parents
とtag.children
のそれぞれが異なるタグのリストを返すように、parentタグとchildrenタグを持つタグが必要です。Ectoでheirarchicalタグを使用する方法
これは自分のクエリを記述しなくても可能ですか?
tag.parents
とtag.children
のそれぞれが異なるタグのリストを返すように、parentタグとchildrenタグを持つタグが必要です。Ectoでheirarchicalタグを使用する方法
これは自分のクエリを記述しなくても可能ですか?
できました。私が最初に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)
との関係を作成することができます