title: rails devise: user and account system
body: <p>I've been trying to add an account to a user for a couple of days now.</p>
  
  <p>I've installed Devise with no problems, but now I wanted to add a <strong>Company Name</strong> field to my registration form.</p>
  
  <p>Here's my code:</p>
  
  <h1>app/models/account.rb</h1>
  
  <pre><code>class Account &lt; ActiveRecord::Base
     attr_accessible :name
     has_many :users
     accepts_nested_attributes_for :users
  end
  </code></pre>
  
  <h1>app/models/user.rb</h1>
  
  <pre><code>class User &lt; ActiveRecord::Base
  
     belongs_to :account
  
     devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable, :confirmable
  
     attr_accessible :first_name, :last_name, :account_attributes, :account, :email, :password, :password_confirmation, :remember_me
  
  end
  </code></pre>
  
  <h1>app/views/devise/registrations/new.html.erb</h1>
  
  <pre><code>&lt;%= form_for(resource, :as =&gt; resource_name, :url =&gt; registration_path(resource_name)) do |f| %&gt;
     &lt;%= devise_error_messages! %&gt;
  
     &lt;div&gt;&lt;%= f.label :first_name %&gt;&lt;br /&gt;
     &lt;%= f.text_field :first_name %&gt;&lt;/div&gt;
  
     &lt;div&gt;&lt;%= f.label :last_name %&gt;&lt;br /&gt;
     &lt;%= f.text_field :last_name %&gt;&lt;/div&gt;
  
     &lt;%= f.fields_for :account do |account_form| %&gt;
        &lt;div&gt;&lt;%= account_form.label :name %&gt;&lt;br /&gt;
    &lt;%= account_form.text_field :name %&gt;&lt;/div&gt;
     &lt;% end %&gt;
  
     &lt;div&gt;&lt;%= f.label :email %&gt;&lt;br /&gt;
     &lt;%= f.email_field :email %&gt;&lt;/div&gt;
  
     &lt;div&gt;&lt;%= f.label :password %&gt;&lt;br /&gt;
     &lt;%= f.password_field :password %&gt;&lt;/div&gt;
  
     &lt;div&gt;&lt;%= f.label :password_confirmation %&gt;&lt;br /&gt;
     &lt;%= f.password_field :password_confirmation %&gt;&lt;/div&gt;
  
     &lt;div&gt;&lt;%= f.submit "Sign up" %&gt;&lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>This is all the code I have. I haven't changed anything more.</p>
  
  <p>And this is the error I got:</p>
  
  <pre><code>Account(#2175324980) expected, got ActiveSupport::HashWithIndifferentAccess(#2169631580)
  </code></pre>
  
  <p>Thanks in advance.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I've found the solution:</p>
  
  <p>I've changed</p>
  
  <pre><code>&lt;%= f.fields_for :account do |account_form| %&gt;
  </code></pre>
  
  <p>To</p>
  
  <pre><code>&lt;%= f.fields_for :account_attributes, resource.account do |account_form| %&gt;
  </code></pre>
  
  <p>Now I'm trying to add a :plan to account_form and I'm getting:</p>
  
  <pre><code>Can't mass-assign protected attributes: plan
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>Ok, rookie mistake. I've just added :plan to account model.</p>
  
  <p>After that I was getting an query error, I just changed some fields in my DB from null => false to null => true and voila.</p>