deerob4
Passing primary key down to nested struct using cast_assoc
I’m using cast_assoc/3 to cast some nested associations in my changesets. The simplified schemas look like this:
schema "trial" do
has_one :chief_investigator, ChiefInvestigator
has_many :principal_investigators, PrincipalInvestigator
end
schema "chief_investigators" do
belongs_to :user, User
belongs_to :trial, Trial
end
schema "principal_investigators" do
belongs_to :user, User
belongs_to :trial, Trial
end
schema "users" do
has_many :accounts, Account
end
schema "accounts" do
belongs_to :user, User
belongs_to :trial, Trial
end
Which all put together form an association that follows this structure:
%Trial{
chief_investigator: %ChiefInvestigator{
user: %User{
accounts: [%Account{}]
}
},
principal_investigators: [
%PrincipalInvestigator{
user: %{
accounts: [%Account{}]
}
}
]
}
My problem is that the trial primary key is not being passed down to the %Account{} schemas. The correct user key is being passed, but not the trial one. Is there something in how I’ve designed the schema that’s impeding this? I could retrospectively insert the key after insertion, but that would be messy given how nested the data is.
Popular in Questions
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it.
I’m very interested in Elixir,...
New
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”:
14:57:30.512 [warn] ...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Other popular topics
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
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
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
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
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New







