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

<p><%= label_tag 'First name' %> <%= f.text_field :firstname %></p>

<p><%= label_tag 'Surname' %> <%= f.text_field :surname %></p>

<p><%= label_tag 'email' %> <%= f.text_field :email %></p>

<p><%= label_tag 'password' %> <%= f.password_field :password %></p>

<p><%= label_tag 'password_confirmation', 'Confirm Password' %> <%= f.password_field :password_confirmation %></p>

  <h2>Company Information</h2>   <% f.fields_for :companies do |company| %>        <%= render 'companies/fields', :f => company %>    <% end %>

<p class="submit"><%= submit_tag 'Sign up' %></p> <% 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"=>"asdf@asdf.com"}}

Show session dump

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.

[snip]

Parameters:

{"commit"=>"Sign up", "user"=>{"password_confirmation"=>"12341234", "companies"=>{"name"=>"1234"}, "firstname"=>"driver", "surname"=>"asdf", "password"=>"12341234", "email"=>"a...@asdf.com"}}

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 Jones

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>

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

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

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

    <p><label for="password_confirmation">Confirm Password</label>     <input id="user_password_confirmation" name="user [password_confirmation]" size="30" type="password" /></p>

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