Adding value in 2 tables

I currently have a registration form.
Working perfectly.

However I added a new table
and it needs to be populated automatically

In my form template I have.

  <div class="form-group">
    <%= label f, :tel_cf, class: "control-label" %>
   <%= text_input f, :tel_cf, class: "form-control" %>
    <%= error_tag f, :tel_cf %>
  </div>

By clicking
<%= submit "Submit", class: "btn btn-primary" %>

add in: :tel_cf and :table2_tel_cf

Is there any way to add this to my template?
Or would it be a controller?
If it is controller, how would it be?

Are these fields in a database or in an html <table>? If it’s the former, use a transaction or a multi.

In my case,
it’s in a database

Then you can use a transaction or a multi.

With transaction:

Repo.transaction(fn ->
  # insert in tel_cf
  # insert in table2_tel_cf
end)

With multi:

alias Ecto.Multi

Multi.new()
|> Multi.insert(:tel_cf, tel_cf_changeset)
|> Multi.insert(:table2_tel_cf, table2_tel_cf_changeset)
|> Repo.transaction()
3 Likes

I believe you are saying there is only one field that you want to record in two different tables. That would mean the template is probably not the right place to do this. Like @idi527 said, transaction or multi would be what you want. As to where to put it, if you are using phoenix contexts, it would go in the one that contains the schema you are updating. If you’re not using contexts, you would probably put it in your controller.

Thank you very much.
That was it.
I was able to insert the field using multi.