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

标签 ruby-on-rails ruby activerecord migration

我看到很多关于 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 to carry seed data. They’re too temporal in nature to do that reliably. Migrations are here to bring you from one version of the schema to the next, not to create a fresh schema from scratch—we have the db/schema.rb file for that.

So, as soon as you actually get going with a real application, people won’t be running your early migrations when they set up the application. They’ll start from whatever version is stored in db/schema.rb and ignore all those previous migrations. This means that any data created by the migrations never make it into the database, so you can’t rely on it.

There are many alternative ways to have more permanent seed data. The easiest is probably just to create a new file in db/seed.rb, which contains those Product.create calls that’ll do the setup. This file can then be called after rake db:schema:load creates the initial schema.

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

相关文章:

jquery - 使用 Jquery 将 Javascript 变量发送到 Rails 3 Controller

ruby - ruby 如何解压传递给 Proc 的参数?

ruby-on-rails - Ruby On Rails - 无法通过报复在 has_many 中执行 where 语句

php - yii ActiveRecord 总是查询数据库

ruby-on-rails - 我们如何使用 Bunny gem 和 RabbitMQ 设置延迟排队?

ruby-on-rails - RSpec Controller 测试未生成正确的 url

javascript - jquery 不在 chrome 中运行,在 ff 中运行

ruby-on-rails - Rails 轮数

ruby - 如何使用交互式 Ruby shell 中的模块?

ruby-on-rails - 为相关数据库创建Rails下拉列表