I have the following 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 am trying to insert a record in all 3 tables as follows:
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'
}
}
}])
The record is inserted in the table users, but not in the tables user_profilesor contacts. The error also does not occur.
What is the right way to do this?
solvable
(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])