Hello,
I need to preload an association on a get_by function, everything seems good but in the response it tell me there is no association
Here’s the concerned code :
Schema of the association :
schema "users_cryptos" do
belongs_to :user, Accounts.User
belongs_to :crypto, Currencies.Crypto
timestamps()
end
@doc false
def changeset(user_crypto, attrs) do
user_crypto
|> cast(attrs, [:user_id, :crypto_id])
|> validate_required([:user_id, :crypto_id])
|> unique_constraint(:unique_subs, name: :subs_index)
end
Controller calling the function :
def index_by_user(conn, %{"user_id" => user_id}) do
preloads = [:cryptos, :users]
user_cryptos = Accounts.get_user_crypto_from_user!(user_id, preloads)
render(conn, "index.json", users_cryptos: user_cryptos)
end
Function in the context :
def get_user_crypto_from_user!(user_id, preloads) do
from(uc in UserCrypto, where: uc.user_id == ^user_id)
|> Repo.all
|> Repo.preload(preloads)
end
If anyone sees the problem please take meout of Elixir preloads :')






















