defmodule User do
schema "users" do
has_one :channel, Channel
field :email, :string
field :first_name, :string
field :last_name, :string
timestamps()
end
end
defmodule Channel do
use Ecto.Schema
import Ecto.Changeset
schema "channels" do
field :strength, :integer
belongs_to :user, User
timestamps()
end
In my App, there cannot exist a channel without a user, so the user_id field on channel is non-null, unique and references the user’s id.
When I insert a user, I would also like to insert a channel for him.
My current code looks as follows:
I want to make this in one shot. I know there will always be two inserts but I’d like to find out if there’s a more elegant way to do this ( In Rails, there is a build( ) method for associated records to be called, and ActiveRecord takes care of it).
I have checked out put_assoc, cast_assoc and build_assoc but looks like they are not going to do the job for me.
Can someone help me with this?
I found the answer, looks like we can use put_assoc here.
This got me confused as I always read online that we use “put_assoc” only when we have the associated record “ready”, although in my case the “user” is not really “ready”.
The code:
Well I’m using all the defaults to create the channel so i don’t even need to put in an empty map, and putting an empty map just to make it work doesn’t seem all that elegant