Struggling to get Belongs_to and Has_many associations to work in Phoenix project

  1. Sorry, but with such attitude changing language would not help you. Changing language because you did not liked one forum response does not looks promising regardless of context.

  2. Look that for others it does not matter if you spend few days or weeks. You have posted today and already received reply. We can’t travel in time and help you in all of those days before you have posted.

  3. Rushing with a first project without understanding how things you use works is not the best idea. You should learn it first and ask questions if you don’t understand something. You can try to search for learning resources by yourself for example in Learning Resources category and as always you are welcome to ask for a help for finding a best resource for you.

  4. @kokolegorille didn’t give you half of the information. It’s an answer full of keywords. He described two ways how to do it. Look that copy-paste whole ecto documentation isn’t also something you want, right? The hexdocs documentation have a search field. If you type for example cast even without pressing you would instantly get an option to navigate to Ecto.Changeset.cast/4 function. The second way have even link to build_assoc documentation.

  5. Giving just a hint instead of providing a complete working example is a good practice. When I’m sending an example code I’m also adding lots of comments and I’m linking a helpful resources, so others may learn more than what I wrote. Simply you would not learn much from copy-paste.

  6. No matter if you are on Elixir, Ruby on any other forum nobody is obligated to do a work for you. The primary thing is to educate by providing a generic answers, so many readers may found our answers helpful.


Besides above let’s focus on your problem … Correct me if I’m wrong, but most probably you started learning full phoenix environment without touching ecto first? Look that you can use phoenix to work with other libraries as well as even without ecto, so it simply does not makes sense to learn ecto from phoenix documentation, right?

So it should be obvious to look for ecto associations guide to understand how ecto and associations works. I would start with those guides:
https://hexdocs.pm/ecto/getting-started.html
https://hexdocs.pm/ecto/constraints-and-upserts.html
https://elixirschool.com/en/lessons/ecto/associations

Have in mind that simply some things changed. For example in elixircasts video they are using old templates with <%= form for=… %> call instead of heex templates which are relatively new features, but it should not cause any problem especially if you test your code in iex shell.

Also you may find this interesting:

In very different example I show how to setup ecto-only environment in one Elixir script file. If you did not know what it is then just give it a try simply by writing such example to file and calling it using following command elixir example.exs in your shell. :smiling_imp:

Let us know if you don’t understand something…

3 Likes