如何改变你的Rails应用程序的数据? [英] How do you change your Rails App Data?

查看:116
本文介绍了如何改变你的Rails应用程序的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了关于ActiveRecord的迁移,以及是否应该被用来在应用程序中改变数据,有些人说是有的说没有一个谈了很多。我的问题是,如果你不使用的迁移什么是您使用要做到这一点呢?只是另一个脚本,你写的?

I have seen a lot of talk regarding ActiveRecord Migrations and whether or not they should be used to change data within your application, some people saying yes some saying no. My question is if you are not using Migrations to do this then what are you using? Just another script that you write?

我的替代方式suggestitions后,为什么他们可能会比只用迁移的一个更好的主意。

I am after suggestitions on alternative ways and why they might be a better idea than just using migrations.

推荐答案

如果你用一个问题来提供的

One problem comes if you use the provided

rake db:reset

rake db:schema:load

任务,它使用 schema.rb 为基础,建立数据库。因此,没有数据被加载,你就完蛋了。

tasks, which use schema.rb as the basis for setting up your database. So no data gets loaded and you're stuck.

敏捷Web开发使用Rails,第三版 ,你应该得到(?如果红宝石的书是镐的书,这算是吊床的书,顺便说一句),如果你还没有这样做的话,DHH说:

In Agile Web Development with Rails, Third Edition, which you should get (if the Ruby book is the "Pickaxe" book, should this be the "Hammock" book, btw?) if you haven't done so already, DHH says:

...迁移是不是真的意味着   携带种子数据。他们太时间   在自然界中这样做可靠。   迁移来这里是为了从您带来   架构的一个版本到下一个,   不要创建一个新模式   划痕,我们有DB / schema.rb文件   为了那个原因。

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

所以,只要你   得到了实际应用去,   人们不会运行你的早期   迁移时,他们成立了   应用。他们会从开始   任何版本存储在   DB / schema.rb而忽略所有   previous迁移。这意味着   由移民创建的任何数据   从来没有让到数据库中,所以   你不能依赖它。

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.

有很多   替代方式有更多的   永久种子数据。最简单的是   可能只是创建一个新的文件   DB / seed.rb,里面装着   Product.create调用会做   建立。这个文件可以被称为   后耙分贝:模式:负载创建   最初的架构。

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.

这篇关于如何改变你的Rails应用程序的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆