如何 db:seed 模型及其所有嵌套模型?
我有这些类:
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
我试图将一条记录插入到所有 3 个表中,如下所示:
consumer = User.create!(
[{
:email => '[email protected]',
: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'
}
}
}])
一条记录插入到 users
表中,但不插入 user_profiles
或 >联系人
表。也不会出现错误。
做这样的事情的正确方法是什么?
已解决 (感谢@Austin L. 提供的链接)
params = { :user =>
{
:email => '[email protected]',
: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])
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 => '[email protected]',
: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 => '[email protected]',
: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])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的用户模型需要设置为通过
accepts_nested_attributes
接受嵌套属性,请参阅 Rails 文档以获取更多信息和示例:http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
编辑:您也可能需要考虑使用
has_one :contact, :通过=> :user_profile
这将允许您访问联系人,如下所示:@contact = User.first.contact
。编辑 2:在使用rails c
后,我能找到的最佳解决方案是:编辑 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 inrails c
the best solution I can find is this:Edit 3: See the question for a better solution.