Accepts_nested_attributes_for :has_many :through => 'bug?'

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:inreplace’
/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:inreplace’
/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:insend’
/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:ineach’
/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:ininitialize’
/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:inperform_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:inperform_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:inms’
/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:inperform_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:inperform_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:inprocess_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:inprocess’
/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:incall’

/Users/mark/Programming/ruby/s/app/controllers/users_controller.rb:
27:in new' /Users/mark/Programming/ruby/s/app/controllers/users_controller.rb: 27:increate’
/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:indispatch’
/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:inbuild_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:incall’

/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:inreplace’
/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:inreplace’
/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:insend’
/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:ineach’
/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:ininitialize’
/Users/mark/Programming/ruby/s/app/controllers/users_controller.rb:
27:in new' /Users/mark/Programming/ruby/s/app/controllers/users_controller.rb: 27:increate’
/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:inperform_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:inperform_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:inms’
/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:inms’
/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:inperform_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:insend’
/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:inprocess’
/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:incall’
/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:indispatch’
/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:inbuild_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:incall’
/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:incache’
/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:incall’
/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:incall’
/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:incall’
/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:incall’
/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:incall’
/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:inrun’
/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:inprocess_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:instart_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:insafe_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:insend
/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:instart_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:instart’
/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:inlookup_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:insynchronize’
/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:inspawn_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:inhandle_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:inmain_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”=>“[email protected]”}}

Show session dump


Response

Headers:

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


Thanks for reading. sorry about the length.

On Jan 25, 7:34 pm, mark [email protected] 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”=>“[email protected]”}}

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.

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" />