# Grouped\_collection\_select for has\_many :through Association not Updating

**URL:** <https://www.ruby-forum.com/t/grouped-collection-select-for-has-many-through-association-not-updating/237355>\
**Category:** Rails\
**Created:** [November 7, 2013, 10:40pm UTC](https://www.ruby-forum.com/t/grouped-collection-select-for-has-many-through-association-not-updating/237355 "2013-11-07T22:40:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [November 7, 2013, 10:40pm UTC](https://www.ruby-forum.com/t/grouped-collection-select-for-has-many-through-association-not-updating/237355/1 "2013-11-07T22:40:15Z")

</div>

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.

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**

My relationship for Industries and Keywords are:

* * *

class IKeywordable \< ActiveRecord::Base

attr\_accessible :industry\_id, :keyword\_id

belongs\_to :industry  
belongs\_to :keyword

end

class CKeywordable \< ActiveRecord::Base  
attr\_accessible :conference\_id, :keyword\_id

belongs\_to :conference  
belongs\_to :keyword  
end

* * *

Here are the Models:

* * *

class Industry \< ActiveRecord::Base  
attr\_accessible :name,  
:conferences\_attributes,  
:i\_keywordables\_attributes,  
:keywords,  
:keywords\_attributes,  
:keyword\_ids

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

# Associations

has\_many :conferences  
accepts\_nested\_attributes\_for :conferences

#Keywords  
has\_many :i\_keywordables  
has\_many :keywords, through: :i\_keywordables  
accepts\_nested\_attributes\_for :i\_keywordables  
accepts\_nested\_attributes\_for :keywords

end

class Keyword \< ActiveRecord::Base

attr\_accessible  
:name, :profile\_id, :active, :rating,  
:c\_keywordables\_attributes,  
:i\_keywordables\_attributes,  
:industries\_atributes

# Associations

#Industries  
has\_many :i\_keywordables  
has\_many :industries, through: :i\_keywordables  
accepts\_nested\_attributes\_for :i\_keywordables

#Conferences  
has\_many :c\_keywordables  
has\_many :conferences, through: :c\_keywordables  
accepts\_nested\_attributes\_for :c\_keywordables

end

class Conference \< ActiveRecord::Base  
attr\_accessible  
: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

#Keywords  
has\_many :c\_keywordables  
has\_many :keywords, through: :c\_keywordables  
accepts\_nested\_attributes\_for :c\_keywordables

serialize :keywords, Array

def to\_s  
state\_id  
end

end

* * *

Here is the Conference/edit.html.erb

* * *

## edit \<%= @conference.name %\>
\<%= 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 %\>
* * *
\<%= c.label :industry\_id %\> \<%= c.collection\_select :industry\_id, Industry.order(:name), :id, :name %\>  
\<%= c.fields\_for :industry, @industry do |ci| %\> \<%= ci.label :keyword\_id %\> \<%= ci.grouped\_collection\_select :keywords, Industry.order(:name), :keywords, :name, :id, :name, {:include\_blank =\> true}, { :multiple =\> true } %\> \<% end %\>  

\<%= c.submit %\>

\<% end %\> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

Here is the Conference Controller:

* * *

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

def update  
@conference = Conference.find(params[:id])  
if @conference.update\_attributes(params[:conference])  
redirect\_to action: “all”, notice: ‘conference was successfully  
updated.’  
else  
render action: “edit”  
end  
end

* * *

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”=\>“removed\_email\_address@domain.invalid”, “web”=\>“[www.example.com](http://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!!
