在 seeds.rb 中创建模型及其子模型



我有两个模型:GodGodSkin

class God < ActiveRecord::Base
  has_many :god_skins
end
class GodSkin < ActiveRecord::Base
  belongs_to :god
end

我正在尝试创建一些种子数据,特别是God记录和许多子记录GodSkin记录。

God.create!(
    name: 'Agni',
    title: 'God of Fire',
    lore: 'There are few elements as destructive or as purifying as fire. Agni, God of Fire, is
           the embodiment of both of these qualities, with a head for each. Though the source of
           his origin warrants debate - for there are many tales of his parentage ranging from two
           simple sticks rubbed together, to the cosmic energy that made all things at the beginning
           of time - Agni is a pivotal and important God with many duties to the Pantheon. He is the
           twin brother to Indra, God of the Heavens and Rains and chief among warriors. Conversely,
           Agni is chief among priests, acting as messenger between mortals and Gods. Every Hindu ritual
           and prayer is performed in front of a fire of some kind, so Agni carries the words and sacrifices,
           traveling between the Earth and the Heavens. He is welcome in every home and every hearth and
           much beloved by the Faithful. Through his flames, Agni provides heat and light, but also cleanses
           impurities. Smoke from his pyres create the air and hold the Heavens aloft. The sun, a source of
           fire itself, brings life-giving energy to the world, and his lightning streaks the sky during storms.
           For all his kindness and service, Agni has two faces. One is the face of kindness and purity,
           turned towards the people and Gods. His other face, grim and resolute, guides the God of Fire,
           to play his role in the cosmic cycle of creation and destruction, to burn and blacken all the
           atrocities of the world to ash.',
    pros: 'High Single Target Damage',
    cons: 'Low Defense',
    tags: 'Melee,Tank',
    release_date: Date.parse('Oct 4, 2012'),
    god_skins: {
        god_skin: {
            name: 'test',
            url: 'http://www.google.com',
            price: '5000'
        },
        god_skin: {
            name: 'test2',
            url: 'http://www.google.com/asdas',
            price: '2300'
        }
    }
)

运行时rake db:seed我收到错误GodSkin expected, got an Array.

我做错了什么?

您需要告诉您的模型,它可以通过accepts_nested_attributes_for :god_skins创建其子项。然后,您只需要将密钥从god_skins重命名为god_skins_attributes,一切都应该可以正常工作。

更新:

还有一个更改,而不是传递带有重复键 (!!!!!) 的哈希,而是使用哈希数组:

god_skins_attributes: [
    {
        name: 'test',
        url: 'http://www.google.com',
        price: '5000'
    },
    {
        name: 'test2',
        url: 'http://www.google.com/asdas',
        price: '2300'
    }
}

另一种解决方案:

如果出于任何原因您不想使用嵌套属性,则可以在创建父属性后创建子属性:

God.create!(
  name: 'Agni',
  title: 'God of Fire',
  ...
).god_skins.create!([
  {
    name: 'test',
    url: 'http://www.google.com',
    price: '5000'
  },
  god_skin: {
    name: 'test2',
    url: 'http://www.google.com/asdas',
    price: '2300'
  }
])

但是,这可能会创建父级,

然后在创建子级时失败,从而使父级在您的数据库中没有子级。为了防止这种情况,您需要将这些调用包装在事务中。

最新更新