Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9217477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:38:32+00:00 2026-06-18T02:38:32+00:00

I have a Sezzion model: attr_accessible :description has_many :session_instructors, :dependent => :destroy has_many :instructors,

  • 0

I have a Sezzion model:

attr_accessible  :description
has_many :session_instructors, :dependent => :destroy
has_many :instructors, :through => :session_instructors
accepts_nested_attributes_for :session_instructors
accepts_nested_attributes_for :instructors

Instructor model:

attr_accessible :bio
has_many :sezzions, :through => :session_instructors
has_many :session_instructors, :dependent => :destroy

SessionInstructor model:

attr_accessible :instructor_id, :sezzion_id
belongs_to :sezzion
belongs_to :instructor

Lastly, User model:

has_many :sezzions
has_many :instructors

I’m trying to create a form for Sezzion with nested form for SessionInstructor which has multiple select option for Instructors.

How can I do the following:

  1. nested form for SessionInstructor
  2. use multiple select option to get all the selected Instructor‘s instructor_id
  3. hidden field to pass in the created/updated session_id with each select instructor

I have the following code as of now:

<%= form_for(@sezzion) do |f| %>

    <%= f.label :description %>
    <%= f.text_area :description %>


    <%= f.label :instructors %>
    <%= fields_for :session_instructors do |f| %>
      <select multiple>
        <% current_user.instructors.each do |instructor| %>
          <option><%= instructor.name %></option>
        <% end %>
      </select>
    <% end %>

    <%= f.submit %>

<% end %>

Thank you so much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:38:33+00:00Added an answer on June 18, 2026 at 2:38 am

    This is something that seems ridiculously hard in Rails.

    I think something like this might work:

    <%= f.fields_for :session_instructors do |si| %>
      <%= si.collection_select :instructor_ids, current_user.instructors, :id, :name, multiple: true>
    <% end %>
    

    This should create a form element which will set sezzion[session_instructors_attributes][instructor_ids].

    Although I’m not sure if that’s actually what you want. I’ve never tried this using a multi select. If it doesn’t work, you could also try getting rid of the fields_for and just using f.collection_select. If you’re willing to use a checkbox, I can show you how to do that for sure.

    I hope that helps.

    Edit:

    Here’s how I would usually do it with a check_box:

    <%= f.fields_for :session_instructors do |si| %>
      <%= si.hidden_field "instructor_ids[]" %>
      <% current_user.instructors.each do |i| %>
        <%= si.check_box "instructor_ids[]", i.id, i.sezzions.include?(@sezzion), id: "instructor_ids_#{i.id}" %>
        <%= label_tag "instructor_ids_#{i.id}", i.name %>
      <% end %>
    <% end%>
    

    There are a couple “gotchas!” with this method. When editing a model, if you deselect all checkboxes then it won’t send the parameter at all. That’s why the hidden_field is necessary. Also, you need to make sure each form element has a unique id field. Otherwise only the last entry is sent. That’s why I manually set the value myself.

    I copy pasted and then edited. Hopefully I got the syntax close enough where you can get it to work.

    FINAL EDIT:

    Per Sayanee’s comment below, the answer was a bit simpler than I thought:

    <%= f.collection_select :instructor_ids, current_user.instructors, :id, :name, {}, {:multiple => true} %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have started working my way through a book called WebGL: Up and Running, which
Have created a ATL COM project through which I am inserting Menu Items to
Have been trying to find a good description of what the next() method does.
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Have done quite a bit of searching for a guide (of any substance) for
Have deployed numerous report parts which reference the same view however one of them
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
Have a painfully simple blog Post creator, and I'm trying to check if the
Have an issue with marshall and unmarshall readers and writers. So here it is.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.