如何 db:seed 模型及其所有嵌套模型?

发布于 2024-10-07 01:29:33 字数 2044 浏览 3 评论 0原文

我有这些类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离不开的别离 2024-10-14 01:29:33

您的用户模型需要设置为通过 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 后,我能找到的最佳解决方案是:

@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)

编辑 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文