Accepts nested attributes using mongoid for has_one relationship

I'm a little new to using Mongoid, but have significant experience with ActiveRecord. I have the following models

def Company
  field :name

  has_one :owner, autosave: true, class_name: 'User', inverse_of: :company
  accepts_nested_attributes_for :owner
end

def User
  belongs_to :company, inverse_of: :owner
  has_one :profile
end

My RegistrationController has the following method

def new 
  @company = Company.new
  @company.build_owner
  @company.owner.build_profile
  respond_with @company
end 

And in my opinion ...

= simple_form_for @company, url: user_registration_path do |f| 
  = f.error_notification
  .inputs
    = f.simple_fields_for @company.owner do |o| 
      = o.input :email, required: true, autofocus: true
      = o.simple_fields_for @company.owner.profile do |p| 
        = p.input :first_name, required: true
        = p.input :last_name, required: true
      = f.input :name, label: 'Company Name'
      = f.input :subdomain
      = o.input :password, required: true
      = o.input :password_confirmation, required: true
  .actions
    = f.button :submit, "Sign up"

Whenever I submit this form, the parameters are returned in such a way that:

{"utf8"=>"✓",
 "authenticity_token"=>"6z8+evYUwZwx3iADFewsMHiPl00vT7Eq6WaD8BOnQBc=",
 "company"=>
  {"user"=>
    {"email"=>"testing@testing.com",
     "profile"=>{"first_name"=>"testing", "last_name"=>"testing"},
     "password"=>"testing",
     "password_confirmation"=>"testing"},
   "name"=>"testing",
   "subdomain"=>"testing"},
 "commit"=>"Sign up",
 "action"=>"create",
 "controller"=>"users/registrations"}

-, , : user, : user_attributes : owner_attributes? -, , . -, company = Company.new(params [: company]) company.owner , . , company.user . , : ( : ), . . , - ? !

+3
1

owner, .

, user, , mongoid - company[user]. .

simple_form, - f.fields_for :owner, , .

0

All Articles