boggsey

boggsey

Saving an association from a select input

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!

Marked As Solved

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.

Also Liked

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

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

Last Post!

boggsey

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.

Where Next?

Popular in Questions Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
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
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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

We're in Beta

About us Mission Statement