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: https://github.com/phoenixframework/phoenix_ecto/blob/master/lib/phoenix_ecto/html.ex#L315

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

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement