gpt4 book ai didi

ruby-on-rails - 如何更改 Rails 应用程序数据?

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

我看到很多关于 ActiveRecord 迁移以及是否应该使用它们来更改应用程序中的数据的讨论,有些人说是,有些人说不。我的问题是,如果您不使用迁移来执行此操作,那么您在使用什么?只是您编写的另一个脚本?

我正在寻求关于替代方法的建议,以及为什么它们可能比仅使用迁移更好。

最佳答案

如果你使用提供的,就会出现一个问题

rake db:reset

rake db:schema:load

任务,它使用 schema.rb 作为设置数据库的基础。所以没有数据被加载,你被卡住了。

Agile Web Development with Rails, Third Edition ,你应该得到它(如果 Ruby 书是“Pickaxe”书,顺便说一下,这应该是“Hammock”书吗?)如果你还没有这样做,DHH 说:

...migrations aren’t really meant tocarry seed data. They’re too temporalin nature to do that reliably.Migrations are here to bring you fromone version of the schema to the next,not to create a fresh schema fromscratch—we have the db/schema.rb filefor that.

So, as soon as you actuallyget going with a real application,people won’t be running your earlymigrations when they set up theapplication. They’ll start fromwhatever version is stored indb/schema.rb and ignore all thoseprevious migrations. This means thatany data created by the migrationsnever make it into the database, soyou can’t rely on it.

There are manyalternative ways to have morepermanent seed data. The easiest isprobably just to create a new file indb/seed.rb, which contains thoseProduct.create calls that’ll do thesetup. This file can then be calledafter rake db:schema:load creates theinitial schema.

关于ruby-on-rails - 如何更改 Rails 应用程序数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/750307/

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