1player

1player

Idiomatic way to duplicate an Ecto.Changeset

In my CRUD app I would like to add a duplicate feature, in which an existing object’s data is used to prepopulate the form. The naive way I started with was:

def duplicate(conn, %{"id" => item_id}) do
  item = Repo.get!(Item, item_id)
  changeset = Ecto.Changeset.change(item)

  render(conn, "new.html", changeset: changeset)
end

where new.html renders a form with form_for pointing to the POST method.

BUT this does not work, as phoenix_ecto overrides the POST method in the form by setting the hidden _method field to put, because it’s noticed that the changeset refers to an existing item in the database, as shown here: phoenix_ecto/lib/phoenix_ecto/html.ex at main · phoenixframework/phoenix_ecto · GitHub

What is the idiomatic way of duplicating a Changeset that refers to an actual record, so that a new entry is created, without having to manually remove the __meta__ field from the changeset?

Most Liked

al2o3cr

al2o3cr

I suspect any function on Ecto.Changeset is going to wind up doing things you don’t want - in this case, I’d recommend taking the simplest possible route and writing a function in Item that takes an Item and returns an unsaved Ecto.Changeset:

def duplicate(item) do
  changeset(%Item{}, %{title: item.title})
end

Reasoning:

  • you likely don’t want to copy some fields (ID, naturally; also probably inserted_at etc)

  • you might want to modify specific data - for instance, adding " (copy)" or similar to a title

  • you may need to do additional work to copy some fields (has_many associations, for instance)

LostKobrakai

LostKobrakai

Unset the id (or any other primary key) for the item and it’s essentially a unsaved copy.

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement