freewebwithme

freewebwithme

Creating a User with Profile schema?

Hi!
I want to create a user and also profile schema at the same time.

For example

defmodule MyApp.Accounts.User do
  use Ecto.Schema
  import Ecto.Changeset

  schema "users" do
    field :username, :string
    field :password, Comeonin.Ecto.Password
    field :email, :string

    has_one :profile, MyApp.Accounts.Profile
    timestamps()
  end
end

defmodule MyApp.Accounts.Profile do
  use Ecto.Schema
  import Ecto.Changeset

  schema "profiles" do
    field :phone_number, :string
    field :address, :string
    field :intro, :string

    belongs_to :user, MyApp.Accounts.User
    timestamps()
  end
end

then I got an input from user like this

attrs = %{username: "username",
          password: "secret", 
          email: "email@example.com", 
          phone_number: "2223334444", 
          address: "some address", 
          intro: "Hello world!"}

then in my accounts.ex file

def create_user(attrs) do                                                                                                                                     
     user_changeset = User.changeset(%User{}, attrs)                                                                                                                   
     profile_changeset = Profile.changeset(%Profile{}, attrs)
                                                                  
     user_changeset                                                                                                                                                    
     |> Ecto.Changeset.put_assoc(:profile, profile_changeset)                                                                                               
     |> Repo.insert()                                                                                                                                                   
end    

I think it works…
But is there any better (elixir) way?

Most Liked

OvermindDL1

OvermindDL1

Ecto’s associations assume there is only one join column, but because of legacy reasons I have at minimum 2 join columns and sometimes more, thus making the Ecto associations absolutely useless for me as it assumes a design that cannot exist here. ^.^;

OvermindDL1

OvermindDL1

No that’s pretty much it, although I’d probably use a transaction because I like the reliability of the transaction, but it doesn’t look like it actually matters in this case. :slight_smile:

OvermindDL1

OvermindDL1

I most often use Ecto.Multi’s yes, but then I frequently don’t use associations due to significant limitations of them anyway. :slight_smile:

Nah, you can just run it in a transaction function all that you need to do anyway. ^.^

wolfiton

wolfiton

This should offer you an example from the docs https://hexdocs.pm/ecto/Ecto.Repo.html#c:transaction/2

r8code

r8code

Thanks @wolfiton and @OvermindDL1 for the reply

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New

Other popular topics Top

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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

We're in Beta

About us Mission Statement