Designing a registration with nested attributes does not create an association

I have User and Account models with has_one association and nested attributes.
My problem is that when registering a user through Devise, no account is created and no error is generated in the log. I am using Rails 3.2 with Devise 2.0.4.

user.rb

class User
  include Mongoid::Document

  has_one :account, :inverse_of => :user

  accepts_nested_attributes_for :account

  field :name
  validates_presence_of :name
  validates_uniqueness_of :name, :email, :case_sensitive => false
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :account_attributes

  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
  ...
end

      

account.rb

class Account
  include Mongoid::Document

  belongs_to :user, :inverse_of => :account

  field :name

  validates_presence_of :user
  attr_accessible :name, :user_id

end

      

registration_controller.rb

class RegistrationsController < Devise::RegistrationsController
  def new
    resource = build_resource({})
    resource.build_account
    respond_with resource
  end
end 

      

view

<h2>Sign up</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <%= f.fields_for :account do |account_form| %>
    <div><%= account_form.label 'Company' %>
    <%= account_form.text_field :name %></div>
  <% end %>
...

      

here is my console output

Started POST "/users" for 127.0.0.1 at 2012-03-21 03:40:55 -0700
Processing by RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓",         "authenticity_token"=>"dAmJrthe/IjlHzjBI2kF9nkTxwIWM0o69Q1PI2nd95o=", "user"=>{"account_attributes"=>{"name"=>"comp11"}, "name"=>"user11", "email"=>"uu3@u3.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
MONGODB (1ms) myapp_development['$cmd'].find({"count"=>"users", "query"=>{:email=>"uu3@u3.com"}, "fields"=>nil}).limit(-1)
MONGODB (0ms) myapp_development['$cmd'].find({"count"=>"users", "query"=>{:name=>/^user11$/i}, "fields"=>nil}).limit(-1)
MONGODB (0ms) myapp_development['$cmd'].find({"count"=>"users", "query"=>{:email=>/^uu3@u3\.com$/i}, "fields"=>nil}).limit(-1)
MONGODB (1ms) myapp_development['users'].insert([{"email"=>"uu3@u3.com", "encrypted_password"=>"$2a$10$lY6aHKTyeVALAcIkX.Ipke5YMj7/viU9Hy5s.jsQAq7cfCBtJtXaO", "sign_in_count"=>0, "_id"=>BSON::ObjectId('4f69b0377e3d3639bf000008'), "name"=>"user11"}])
MONGODB (0ms) myapp_development['users'].update({"_id"=>BSON::ObjectId('4f69b0377e3d3639bf000008')}, {"$set"=>{"last_sign_in_at"=>2012-03-21 10:40:56 UTC, "current_sign_in_at"=>2012-03-21 10:40:56 UTC, "last_sign_in_ip"=>"127.0.0.1", "current_sign_in_ip"=>"127.0.0.1", "sign_in_count"=>1}})
Redirected to http://localhost:3000/
Completed 302 Found in 257ms

      

As you can see, I don't see anything about creating an Account . Also verified that the Account collection is not being created by browsing MongoDB. What am I missing? Thank you for your help in advance.

+3


source to share


1 answer


Solved a problem.
I needed to enable: autosave

has_one :account, :inverse_of => :user, :autosave => true

      



mongoid docs pointed out the problem.

Please note that the autosave option must be enabled for relational associations that use accepts_nested_attributes_for if you do not want to manually save the relationship on update.

+2


source







All Articles