4
Ectoモデル/レコードをクローンする最も簡単な方法は何ですか?多くの成分とラベルを埋め込んだサンプルレシピモデルがあります。Ectoレコードをクローンします。埋め込みと関連付けられたレコードでは?
モデル
defmodule App.Recipe do
use App.Web, :model
schema "recipes" do
field :name, :string
has_many :ingredients, App.Ingredient
embeds_many :labels, App.Label
end
クローンレシピレコード はどのようにレシピレコードを複製し、新しいレシピレコードを挿入するためのチェンジセットを作成することができますか?
recipe = Repo.get(App.Recipe, 1)
recipe_changeset = Ecto.Changeset.change(recipe)
# ... Steps for cloning record with embeds?
new_recipe = Repo.insert(recipe_changeset)
クローンレシピや食材と食材に新しいレシピIDを割り当てる
どのように私は新しい食材を使用し、新しいレシピレコードを挿入するためのプリロード食材とレシピレコードのクローンを作成することができますか?
recipe = Repo.get(App.Recipe, 1)
|> Repo.preload(:ingredients)
recipe_changeset = Ecto.Changeset.change(recipe)
# ... Steps for cloning records?
new_recipe = Repo.insert(recipe_changeset)
また、すべての成分をクローンし、 'recipe_id'を新しいレシピのIDに設定しますか? – Dogbert
はい、すべての成分をクローンして、新しいレシピIDを割り当てたいと思います。 – rodeinator