deerob4

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.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement