gpt4 book ai didi

ruby-on-rails - 在 seeds.rb 中创建模型及其子项?

转载 作者:数据小太阳 更新时间:2023-10-29 07:56:10 24 4
gpt4 key购买 nike

我有两个模型: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 创建它的 child 。然后你只需要将 key 从 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'
}
])

)

然而,这可能会创建父项,然后无法创建子项,从而使您的数据库中只有没有子项的父项。为防止发生这种情况,您需要将这些调用包装在事务中。

关于ruby-on-rails - 在 seeds.rb 中创建模型及其子项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20647856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com