boggsey

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!

Showing Posts 1 to 6

shd42

shd42

Hello !

You’re using inputs_for, which is made to create / update embedded schema or relationship. When you declare belongs_to, session his a direct link to the related schema, but it’s not the field available on the students table. That macro automatically adds the foreign key, which by default would be session_id.

So in your case, you need to remove the inputs_for, and replace with the normal select, pointing to session_id, like this (actually, has stated by @antoine, you have changed the foreign_key to bold_session, so that should be the one to use here ):

<%= label sf, :bold_session %>
<%= select sf, :bold_session, @sessions, prompt: "Choose a session" %>
<%= error_tag sf, :bold_session %>

Normally, it should then just apply the selection to the proper column and create the student with the relationship assigned.

antoine

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

%{
    ...
    "session_id" => 5,
    ...
}

Then you simply have to cast it

  @fields [:firstname, :lastname, :session_id]
  @required_fields [:firstname, :lastname, :session_id]

  def changeset(student, attrs) do
    student
    |> cast(attrs, @fields)
    |> validate_required(@required_fields)
  end

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:

  belongs_to :session, Session, foreign_key: :bold_session

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

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_many name to a plural form such as students instead of student. I think it is more readable.

Ex:

has_many :things, Thing
has_many :criteria, Criterion
has_one :profile, Profile
boggsey

boggsey OP

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

boggsey OP

Completely agree - It felt wrong when I was typing out the question. Thanks for the feedback!

boggsey

boggsey OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews