Building has_many :through record and preloading the results - Stuck

Thanks for reading!

I’m just learning Ecto, working on my first phoenix app, and having a hard time understanding the best way to handle a part of a has_many through: relationship in Ecto. Basically, I’m trying to create a parent with both a child and a grandchild record - all at the same time. I have managed to do this using the following code, but the response is not fully preloaded. The child (accounts_users) is present, but the accounts: is an empty list.

Relationship is very standard %User{} >> %AccountUser{} << %Account{} (full schema outlined below). My Abbreviated code to store the records:

# create_user(attrs)
%User{}
    |> Repo.preload([:accounts, :accounts_users])
    |> User.changeset(attrs)
    |> Repo.insert()

# abbreviated changest
accounts_users = Ecto.build_assoc(user, :accounts_users, %{account: %Account{}})

changeset |> put_assoc(:accounts_users, [accounts_users])

When create_user/1 is called, accounts comes back as an empty list, while account_user has the relevant record. If I subsequently reload the record from the DB, both accounts and account_user are correctly preload. My next thought was… Why don’t I try to put the account association into the changeset? I tried several ways to do this, but none successful. In once case, I seem to get a very curious error that seems to hint I have a typo somewhere, but my relationships seem correct (see below)

# added to change set:
|> put_assoc(:accounts, account)

# error message
** (ArgumentError) cannot put assoc `accounts`, assoc `accounts` not found. Make sure it is spelled correctly and that the association type is not read-only

I’m stuck and not quite sure how to handle this. I’d hate to have to subsequently refetch the data after every create. Any help would be appreciated!

Appendix of my abbreviated schemas

schema "users" do
  has_many :accounts_users, MyApp.AccountUser
  has_many :accounts, through: [:accounts_users, :account]
end

schema "accounts_users" do
  belongs_to :account, MyApp.Account
  belongs_to :user, MyApp.User
end

schema "accounts" do
  has_many :accounts_users, MyApp.AccountUser
  has_many :users, through: [:accounts_users, :user]
end