boggsey
Hey friends! I’m very new to Elixir and Phoenix and this community has been extremely helpful so far. Unfortunately, I am running into some issues with my understanding. I’m hoping someone can suggest some reading or point me in the right direction. I’ve found lots of resources that come close to my issue, but I can’t bridge the gap in my understanding.
I have many Students who belong to a single Session.
student.ex schema example
schema "students" do
field :first_name, :string
field :last_name, :string
belongs_to :session, Session, foreign_key: :bold_session
session.ex schema example
schema "sessions" do
field :name, :string
field :active, :boolean, default: false
has_many(:student, Student)
When creating a Student, I need to assign them to a Session. In order to do that, I query all of the available sessions and pass them into a select in the form.
The query looks like this:
def session_query() do
session_query = from(s in Session, select: {s.name, s.id})
Repo.all(session_query)
end
The select looks like this:
<%= inputs_for f, :session, fn sf -> %>
<%= label sf, :session %>
<%= select sf, :session, @sessions, prompt: "Choose a session" %>
<%= error_tag sf, :session %>
<% end %>
Everything renders correctly - However, when I submit the form, the Student is not created.
The student’s session is submitted as:
"session" => %{"session" => "5"}
However, when I inspect the errored changeset in student_controller I get:
session: #Ecto.Changeset<
action: :insert,
changes: %{},
errors: [name: {"can't be blank", [validation: :required]}],
data: #Bold.Sessions.Session<>,
valid?: false
>
Obviously my understanding of Elixir and Phoenix is pretty limited. I’m not understanding what Name has to do with it, although Name is a field of Session. All I want to do is create a Student with a Session and eventually list out all students by session. Any thoughts on what I am missing here? Any help, guidance, tough love, etc is greatly appreciated!
Trending in Questions
Other Trending Topics
Latest Phoenix Threads
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
shd42
Hello !
You’re using
inputs_for, which is made to create / update embedded schema or relationship. When you declarebelongs_to,sessionhis a direct link to the related schema, but it’s not the field available on thestudentstable. That macro automatically adds the foreign key, which by default would besession_id.So in your case, you need to remove the
inputs_for, and replace with the normal select, pointing tosession_id, like this (actually, has stated by @antoine, you have changed the foreign_key tobold_session, so that should be the one to use here ):Normally, it should then just apply the selection to the proper column and create the student with the relationship assigned.
antoine
Thanks @shd42, indeed the select is more appropriate, didn’t know this, I have never used forms.
So if you change your view to use the select instead of the input, you will receive an attrs that looks like this
Then you simply have to cast it
But this is true only if the foreign_key name in your table is named
session_id.Wich seems to not to be the case, because you overrided it:
Any reason for this ? what was the intention ?
If you need this field ne be named
bold_session, just change session_id to bold_session in both changeset and your view.Kurisu
I think the other members have already adressed your concern.
I just want to comment on a little detail.
To follow the usual association naming (of course this is not mandatory ^^) I would suggest to rename the
has_manyname to a plural form such asstudentsinstead ofstudent. I think it is more readable.Ex:
boggsey
Thanks for the feedback. I actually created a column called bold_session to hold the ID in the DB. I did this because I didn’t know the convention. I think I’ll go back and change the structure, as convention makes more sense and I don’t want to go down the rabbit hole of overriding everything.
Also, I was trying to cast_assoc on this, so thanks for the callout!
boggsey
Completely agree - It felt wrong when I was typing out the question. Thanks for the feedback!
boggsey
This makes a lot more sense. I wasn’t understanding exactly how inputs_for and the associations were working. Thanks for the explanation. It really helped clear things up for me.