5

I have these classes:

class User
  has_one :user_profile
  accepts_nested_attributes_for :user_profile
  attr_accessible :email, :password, :password_confirmation, :user_profile_attributes
end

class UserProfile
  has_one :contact, :as => :contactable
  belongs_to :user
  accepts_nested_attributes_for :contact
  attr_accessible :first_name,:last_name, :contact_attributes
end

class Contact
   belongs_to :contactable, :polymorphic => true 
   attr_accessible :street, :city, :province, :postal_code, :country, :phone
end

I'm trying to insert a record into all 3 tables like this:

consumer = User.create!(
  [{
  :email => 'consu@a.com',
  :password => 'aaaaaa',
  :password_confirmation => 'aaaaaa',
  :user_profile => {
      :first_name => 'Gina',
      :last_name => 'Davis',
      :contact => {
        :street => '221 Baker St',
        :city => 'London',
        :province => 'HK',
        :postal_code => '76252',
        :country => 'UK',
        :phone => '2346752245'
    }
  }
}])

A record gets inserted into users table, but not into the user_profiles or contacts tables. No error occurs either.

What's the right way to do such a thing?

SOLVED (thanks @Austin L. for the link)

params =  { :user =>
    {
    :email => 'consu@a.com',
    :password => 'aaaaaa',
    :password_confirmation => 'aaaaaa',
    :user_profile_attributes => {
        :first_name => 'Gina',
        :last_name => 'Davis',
        :contact_attributes => {
            :street => '221 Baker St',
            :city => 'London',
            :province => 'HK',
            :postal_code => '76252',
            :country => 'UK',
            :phone => '2346752245'
          }
      }
  }
}
User.create!(params[:user])
Zabba
  • 64,285
  • 47
  • 179
  • 207

1 Answers1

3

Your user model needs to be setup to accept nested attributes via accepts_nested_attributes

See the Rails documentation for more info and examples: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Edit: Also you might want to consider using has_one :contact, :through => :user_profile which would allow you to access the contact like this: @contact = User.first.contact.

Edit 2: After playing around in rails c the best solution I can find is this:

@c = Contact.new(#all of the information)
@up = UserProfile.new(#all of the information, :contact => @c)
User.create(#all of the info, :user_profile => @up)

Edit 3: See the question for a better solution.

Austin Lin
  • 2,545
  • 17
  • 17
  • Yes it is all set up (I already have the views and they are all working fine). But why it fails in db:seed is the problem – Zabba Dec 11 '10 at 23:56
  • did you remove the `accepts_nested_attributes` from your models when posting here? – Austin Lin Dec 11 '10 at 23:57
  • Yes, I posted only the gist of the models. I'll add all the details. Apologies :) – Zabba Dec 12 '10 at 00:00
  • Have you tried creating the contact and user_profile separately and then assigning them to the user? I can post an example if needed. – Austin Lin Dec 12 '10 at 00:04
  • Yes, those work separately, but I was looking for something more elegant.. :) – Zabba Dec 12 '10 at 00:07
  • Thanks for the link, I got it to work! Updated the question with answer just in case any one else needs this. – Zabba Dec 12 '10 at 00:12