borybar

borybar

Issue whilst creating a form based on many to many relation

Hi,

I am trying to create a form in which a user can choose a product from a list, which at the end will subtract calories of a given product from users daily calorie pool.

I am getting this error: key :foods not found in: %Calorie.UsersFoods.UserFood{__meta__: #Ecto.Schema.Metadata<:built, "users_foods">, food: #Ecto.Association.NotLoaded<association :food is not loaded>, food_id: nil, id: nil, inserted_at: nil, updated_at: nil, user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: nil}

What am I doing wrong? This is what I have up until now:

user.ex

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

  schema "users" do
    field :name, :string
    field :username, :string
    field :password, :string, virtual: true
    field :password_hash, :string
    many_to_many(
      :foods,
      Calorie.Products.Food,
      join_through: "users_foods",
      on_replace: :delete
    )

    timestamps()
  end

products/food.ex

defmodule Calorie.Products.Food do
  import Ecto.Query
  import Ecto
  use Ecto.Schema
  import Ecto.Changeset

  schema "foods" do
    field :calories, :integer
    field :name, :string
    many_to_many(
      :users,
      Calorie.Accounts.User,
      join_through: "users_foods",
      on_replace: :delete
    )

    timestamps()
  end

users_foods/user_food.ex

defmodule Calorie.UsersFoods.UserFood do
  use Ecto.Schema
  import Ecto.Changeset

  alias Calorie.Accounts.User
  alias Calorie.Products.Food

  schema "users_foods" do
    belongs_to(:user, User)
    belongs_to(:food, Food)

    timestamps()
  end

  @required_fields ~w(user_id food_id)a
  def changeset(users_foods, params \\ %{}) do
    users_foods
    |> cast(params, @required_fields)
    |> validate_required(@required_fields)
    |> foreign_key_constraint(:user_id)
    |> foreign_key_constraint(:food_id)
    |> unique_constraint([:users, :foods],
      name: :user_id_food_id_unique_index,
      message: "dupa123"
    )

  end
end

Users Food migration

defmodule Calorie.Repo.Migrations.CreateUsersFoods do
  use Ecto.Migration


  def change do
    create table(:users_foods) do
      add :user_id, references(:users)
      add :food_id, references(:foods)
      timestamps()
    end


    create(index(:users_foods, [:user_id]))
    create(index(:users_foods, [:food_id]))

    create unique_index(:users_foods, [:user_id, :food_id], name: :user_id_food_id_unique_index)
  end
end


First Post!

mathieuprog

mathieuprog

The error says

:foods not found in: %Calorie.UsersFoods.UserFood

and indeed we see that :foods is a field inside User schema and not UserFood schema.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
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
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

Other popular topics Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
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

Latest on Elixir Forum

We're in Beta

About us Mission Statement