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!