# Accepts\_nested\_attributes\_for :has\_many :through =\> 'bug?'

**URL:** <https://www.ruby-forum.com/t/accepts-nested-attributes-for-has-many-through-bug/182285>\
**Category:** Rails\
**Created:** [January 26, 2010, 1:36am UTC](https://www.ruby-forum.com/t/accepts-nested-attributes-for-has-many-through-bug/182285 "2010-01-26T01:36:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [January 26, 2010, 1:36am UTC](https://www.ruby-forum.com/t/accepts-nested-attributes-for-has-many-through-bug/182285/1 "2010-01-26T01:36:38Z")

</div>

Hey guys & ladies!

I’ve got the following relationship which i’m trying to get  
accepts\_nested\_attributes to work with. But when i submit my form, it  
looks as if its expecting a Company object, rather than an array of  
companies. which really doesn’t make sense considering its a  
has\_many :relationship.

So what i’m after, is a way to  
accept\_nested\_attributes\_for :companies, so when my users sign up to  
the website, they can also create a company.

-------------- user.rb

class User \< ActiveRecord::Base  
has\_many :company\_users  
has\_many :companies, :through =\> :company\_users

accepts\_nested\_attributes\_for :companies  
end

-------------- company.rb

class Company \< ActiveRecord::Base  
has\_many :company\_users  
has\_many :users, :through =\> :company\_users  
end

-------------- company\_user.rb  
class CompanyUser \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :company  
end

-------------- My form

\<% form\_for :user, :url =\> users\_path do |f| -%\>

\<%= label\_tag 'First name' %\> \<%= f.text\_field :firstname %\>

\<%= label\_tag 'Surname' %\> \<%= f.text\_field :surname %\>

\<%= label\_tag 'email' %\> \<%= f.text\_field :email %\>

\<%= label\_tag 'password' %\> \<%= f.password\_field :password %\>

\<%= label\_tag 'password\_confirmation', 'Confirm Password' %\> \<%= f.password\_field :password\_confirmation %\>

## Company Information
 \<% f.fields\_for :companies do |company| %\> \<%= render 'companies/fields', :f =\> company %\> \<% end %\>

\<%= submit\_tag 'Sign up' %\>

\<% end -%\>

-------------- The error

ActiveRecord::AssociationTypeMismatch in UsersController#create

Company(#2180266380) expected, got Array(#2148226700)

RAILS\_ROOT: /Users/mark/Programming/ruby/s/

/Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active\_record/  
associations/association\_proxy.rb:263:in `raise_on_type_mismatch' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/ associations/association_collection.rb:320:in`replace’  
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active\_record/  
associations/association\_collection.rb:320:in `each' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/ associations/association_collection.rb:320:in`replace’  
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active\_record/  
associations.rb:1322:in `companies=' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/ base.rb:2744:in`send’  
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active\_record/  
base.rb:2744:in `attributes=' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/ base.rb:2740:in`each’  
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active\_record/  
base.rb:2740:in `attributes=' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/ base.rb:2438:in`initialize’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
base.rb:1331:in `send' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ base.rb:1331:in`perform\_action\_without\_filters’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
filters.rb:617:in `call_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ filters.rb:610:in`perform\_action\_without\_benchmark’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
benchmarking.rb:68:in `perform_action_without_rescue' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.4/lib/active_support/ core_ext/benchmark.rb:17:in`ms’  
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.4/lib/active\_support/  
core\_ext/benchmark.rb:17:in `ms' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ benchmarking.rb:68:in`perform\_action\_without\_rescue’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
rescue.rb:160:in `perform_action_without_flash' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ flash.rb:146:in`perform\_action’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
base.rb:532:in `send' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ base.rb:532:in`process\_without\_filters’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
filters.rb:606:in `process' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ base.rb:391:in`process’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
base.rb:386:in `call' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ routing/route_set.rb:437:in`call’

/Users/mark/Programming/ruby/s/app/controllers/users\_controller.rb:  
27:in `new' /Users/mark/Programming/ruby/s/app/controllers/users_controller.rb: 27:in`create’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/  
1.8/benchmark.rb:308:in `realtime' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ dispatcher.rb:87:in`dispatch’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
dispatcher.rb:121:in `_call' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ dispatcher.rb:130:in`build\_middleware\_stack’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
dispatcher.rb:114:in `call' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ dispatcher.rb:108:in`call’

/Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active\_record/  
associations/association\_proxy.rb:263:in `raise_on_type_mismatch' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/ associations/association_collection.rb:320:in`replace’  
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active\_record/  
associations/association\_collection.rb:320:in `each' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/ associations/association_collection.rb:320:in`replace’  
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active\_record/  
associations.rb:1322:in `companies=' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/ base.rb:2744:in`send’  
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active\_record/  
base.rb:2744:in `attributes=' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/ base.rb:2740:in`each’  
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active\_record/  
base.rb:2740:in `attributes=' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/ base.rb:2438:in`initialize’  
/Users/mark/Programming/ruby/s/app/controllers/users\_controller.rb:  
27:in `new' /Users/mark/Programming/ruby/s/app/controllers/users_controller.rb: 27:in`create’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
base.rb:1331:in `send' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ base.rb:1331:in`perform\_action\_without\_filters’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
filters.rb:617:in `call_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ filters.rb:610:in`perform\_action\_without\_benchmark’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
benchmarking.rb:68:in `perform_action_without_rescue' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.4/lib/active_support/ core_ext/benchmark.rb:17:in`ms’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/  
1.8/benchmark.rb:308:in `realtime' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.4/lib/active_support/ core_ext/benchmark.rb:17:in`ms’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
benchmarking.rb:68:in `perform_action_without_rescue' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ rescue.rb:160:in`perform\_action\_without\_flash’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
flash.rb:146:in `perform_action' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ base.rb:532:in`send’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
base.rb:532:in `process_without_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ filters.rb:606:in`process’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
base.rb:391:in `process' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ base.rb:386:in`call’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
routing/route\_set.rb:437:in `call' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ dispatcher.rb:87:in`dispatch’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
dispatcher.rb:121:in `_call' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ dispatcher.rb:130:in`build\_middleware\_stack’  
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active\_record/  
query\_cache.rb:29:in `call' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/ query_cache.rb:29:in`call’  
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active\_record/  
connection\_adapters/abstract/query\_cache.rb:34:in `cache' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/ query_cache.rb:9:in`cache’  
/Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active\_record/  
query\_cache.rb:28:in `call' /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/ connection_adapters/abstract/connection_pool.rb:361:in`call’  
/Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/head.rb:9:in `call' /Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/methodoverride.rb: 24:in`call’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
params\_parser.rb:15:in `call' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ session/cookie_store.rb:93:in`call’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
failsafe.rb:26:in `call' /Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in`call’  
/Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in  
`synchronize' /Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in`call’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
dispatcher.rb:114:in `call' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/ reloader.rb:34:in`run’  
/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action\_controller/  
dispatcher.rb:108:in `call' /Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/rack/ request_handler.rb:92:in`process\_request’  
/Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion\_passenger/  
abstract\_request\_handler.rb:207:in `main_loop' /Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/ railz/application_spawner.rb:400:in`start\_request\_handler’  
/Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion\_passenger/  
railz/application\_spawner.rb:351:in `handle_spawn_application' /Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/ utils.rb:184:in`safe\_fork’  
/Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion\_passenger/  
railz/application\_spawner.rb:349:in `handle_spawn_application' /Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/ abstract_server.rb:352:in` **send** ’  
/Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion\_passenger/  
abstract\_server.rb:352:in `main_loop' /Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/ abstract_server.rb:196:in`start\_synchronously’  
/Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion\_passenger/  
abstract\_server.rb:163:in `start' /Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/ railz/application_spawner.rb:209:in`start’  
/Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion\_passenger/  
spawn\_manager.rb:262:in `spawn_rails_application' /Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/ abstract_server_collection.rb:126:in`lookup\_or\_add’  
/Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion\_passenger/  
spawn\_manager.rb:256:in `spawn_rails_application' /Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/ abstract_server_collection.rb:80:in`synchronize’  
/Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion\_passenger/  
abstract\_server\_collection.rb:79:in `synchronize' /Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/ spawn_manager.rb:255:in`spawn\_rails\_application’  
/Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion\_passenger/  
spawn\_manager.rb:154:in `spawn_application' /Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/ spawn_manager.rb:287:in`handle\_spawn\_application’  
/Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion\_passenger/  
abstract\_server.rb:352:in ` __send__' /Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion_passenger/ abstract_server.rb:352:in`main\_loop’  
/Library/Ruby/Gems/1.8/gems/passenger-2.2.9/lib/phusion\_passenger/  
abstract\_server.rb:196:in `start\_synchronously’  
/Library/Ruby/Gems/1.8/gems/passenger-2.2.9/bin/passenger-spawn-server:  
61

Request

Parameters:

{“commit”=\>“Sign up”,  
“user”=\>{“password\_confirmation”=\>“12341234”,  
“companies”=\>{“name”=\>“1234”},  
“firstname”=\>“driver”,  
“surname”=\>“asdf”,  
“password”=\>“12341234”,  
“email”=\>“removed\_email\_address@domain.invalid”}}

Show session dump

* * *

Response

Headers:

{“Content-Type”=\>"",  
“Cache-Control”=\>“no-cache”}

* * *

Thanks for reading. sorry about the length.

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [January 26, 2010, 8:19pm UTC](https://www.ruby-forum.com/t/accepts-nested-attributes-for-has-many-through-bug/182285/2 "2010-01-26T20:19:22Z")

</div>

On Jan 25, 7:34 pm, mark [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the website, they can also create a company.

[snip]

> Parameters:
> 
> {“commit”=\>“Sign up”,  
> “user”=\>{“password\_confirmation”=\>“12341234”,  
> “companies”=\>{“name”=\>“1234”},  
> “firstname”=\>“driver”,  
> “surname”=\>“asdf”,  
> “password”=\>“12341234”,  
> “email”=\>“removed\_email\_address@domain.invalid”}}

Your models and forms look OK, but something has gone wrong in the  
generated HTML code - the ‘companies’ key should be  
‘companies\_attributes’ to trigger the nested attributes stuff. Can you  
post the generated HTML?

–Matt J.

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [January 26, 2010, 10:18pm UTC](https://www.ruby-forum.com/t/accepts-nested-attributes-for-has-many-through-bug/182285/3 "2010-01-26T22:18:12Z")

</div>

Thanks for having a look Matt, I’ve changed

\<% f.fields\_for :companies do |company| %\>

to

\<% f.fields\_for :companies\_attributes do |company| %\>

and i’m no longer getting the “expected object, but got array error”,  
now i’m getting  
“undefined method `stringify\_keys’ for “test”:String” where “test” was  
the company name i inputted into the form. Below is the html output of  
the form.

```
<form action="/users" class="new_user" id="new_user" method="post">
<p><label for="First_name">First name</label>
<input id="user_firstname" name="user[firstname]" size="30"

```

type=“text” value="" /\>

```
<p><label for="Surname">Surname</label>
<input id="user_surname" name="user[surname]" size="30" type="text"

```

value="" /\>

```
<p><label for="email">Email</label>
<input id="user_email" name="user[email]" size="30" type="text" /></

```

p\>

```
<p><label for="password">Password</label>
<input id="user_password" name="user[password]" size="30"

```

type=“password” /\>

```
<p><label for="password_confirmation">Confirm Password</label>
<input id="user_password_confirmation" name="user

```

[password\_confirmation]" size=“30” type=“password” /\>

```
<hr />
<h2>Company Information</h2>

  <input id="user_companies_attributes_name" name="user

```

[companies\_attributes][name]" size=“30” type=“text” /\>

```
<p class="submit"><input name="commit" type="submit" value="Sign

```

up" /\>
