Jump to content

The ultimate community for Ruby on Rails developers.


grouped_collection_select for has_many :through Association not Updating

association nested form_for

  • Please log in to reply
1 reply to this topic

#1 jemiller1963



  • Members
  • 2 posts

Posted 07 November 2013 - 07:10 PM

I have a scenario where I created a few has_many :through associations which I am having trouble updating. I do appreciate any help! Thank you!


Here is my setup:

  • ruby 1.9.3p392
  • Rails 3.2.13

Here is the scenario:

  • I have 3 models that are related
  • There are many Industries, many Keywords, and many Conferences
  • Industries have many Keywords
  • Conferences have an Industry
  • Conferences choose Keywords based on its Industry

Keywords has a basic CRUD model. Creating an Industry then adding Keywords works fine.

My relationship for Conferences, Industries, and Keywords is where I am having issues. I am trying to update Conference information, change the Industry that it is currently associated with, choose new Keywords based on the new Industry all in the same form. When I remove the Keywords element out of the mix, the update works. But when all three are there, then I get the errors.


My relationship for Industries and Keywords are:

class IKeywordable < ActiveRecord::Base

attr_accessible :industry_id, :keyword_id

belongs_to :industry
belongs_to :keyword

class CKeywordable < ActiveRecord::Base
attr_accessible :conference_id, :keyword_id

belongs_to :conference
belongs_to :keyword

Here are the Models:

class Industry < ActiveRecord::Base
attr_accessible :name, 

validates :name, presence: true
validates :name, uniqueness: true

# Associations
has_many :conferences
accepts_nested_attributes_for :conferences

has_many :i_keywordables
has_many :keywords, through: :i_keywordables
accepts_nested_attributes_for :i_keywordables
accepts_nested_attributes_for :keywords

class Keyword < ActiveRecord::Base

:name, :profile_id, :active, :rating, 

# Associations

has_many :i_keywordables
has_many :industries, through: :i_keywordables
accepts_nested_attributes_for :i_keywordables

has_many :c_keywordables
has_many :conferences, through: :c_keywordables
accepts_nested_attributes_for :c_keywordables

class Conference < ActiveRecord::Base
:name, :address1, :address2, :city, :email,
:web, :profile_id, :state_id, :conference, 
:description, :date_start, :date_end, :venue, :venue_web, 
:organizer, :organizer_web, :c_keywordables_attributes, 
:industry_attributes, :industry_id,
:industry, :keyword_ids

# Associations

belongs_to :state
belongs_to :industry
accepts_nested_attributes_for :industry

has_many :c_keywordables
has_many :keywords, through: :c_keywordables
accepts_nested_attributes_for :c_keywordables

serialize :keywords, Array

def to_s


Here is the Conference/edit.html.erb

<h2>edit <%= @conference.name %></h2>
<%= form_for @conference do |c| %>
<%= c.label :name %>
<%= c.text_field :name %>
<%= c.label :address1 %>
<%= c.text_field :address1 %>
<%= c.label :address2 %>
<%= c.text_field :address2 %>
<%= c.label :city %>
<%= c.text_field :city %>
<%= c.label :state_id%>
<%= c.collection_select :state_id, State.order(:name), :id, :name %>
<%= c.label :email %>
<%= c.text_field :email %>
<%= c.label :web %>
<%= c.text_field :web %>
<hr width="80%">
<%= c.label :industry_id %>
<%= c.collection_select :industry_id, Industry.order(:name), :id, :name %>
<br />
<%= c.fields_for :industry, @industry do |ci| %>
<%= ci.label :keyword_id %>
<%= ci.grouped_collection_select :keywords, Industry.order(:name),
{:include_blank => true}, { :multiple => true }
<% end %>
<br />
<p><%= c.submit %></p>
<% end %>

Here is the Conference Controller:

def edit
@conference = Conference.find(params[:id])

def update
@conference = Conference.find(params[:id])
if @conference.update_attributes(params[:conference])
redirect_to action: "all", notice: 'conference was successfully updated.'
render action: "edit" 

The view renders fine but when I submit a change, I get an error. The error that I am seeing is:

Couldn't find Industry with ID=3 for Conference with ID=1


Here are the parameters passed:

{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"k9fcAXF/iOXt3oVuTcxeDCaWOfE2PrVlBvFbQG8Mo/I=", "conference"=>{"name"=>"test conference", "address1"=>"address 1", "address2"=>"address 2", "city"=>"City", "state_id"=>"38", "email"=>"test@example.com", "web"=>"www.example.com", "industry_id"=>"1", "industry_attributes"=>{"keywords"=>["", "6", "5"], "id"=>"3"}}, "commit"=>"Update Conference", "action"=>"update", "controller"=>"conferences", "id"=>"1"}


Thanks again for any help!!


#2 folubode



  • Members
  • 7 posts

Posted 13 November 2015 - 11:34 PM

am having the same problem. My multi-select variable which is an array is listed like this subject_ids: subject_ids: "---\n- ''\n- '17'\n- '21'\n- '26'\n" when I checked through rails console; it looks like this in development log "subject_ids"=>["", "17", "21", "26"] 


But not displayed in table when I looked in the table using Querious and MySQL Workbench. Any advise?


my form_for for the variable is this:


<%= f.grouped_collection_select :subject_ids, Classroom.order(:name), :subjects, :name, :id, :name, {prompt: "Select subjects"}, {multiple: true} class:"form-control" %>


my controller where I permit the variable is this:


params.require(:subscription).permit(:email, :first_name, :last_name, :mobile, :account_email, :plan_id, :classroom_id, :subject_ids => [])

Also tagged with one or more of these keywords: association, nested, form_for

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users