如何更改 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?
I am after suggestitions on alternative ways and why they might be a better idea than just using migrations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用提供的
和
任务,就会出现一个问题,这些任务使用
schema.rb
作为设置数据库的基础。 所以没有数据被加载并且你被卡住了。在 Rails 敏捷 Web 开发,第三版,你应该得到它(如果 Ruby 书是“Pickaxe”书,这应该是“Hammock”书,顺便说一句?)如果你还没有这样做,DHH 说:
One problem comes if you use the provided
and
tasks, which use
schema.rb
as the basis for setting up your database. So no data gets loaded and you're stuck.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:
很多时候,迁移是最合适的,并且不能用单独的脚本替代。 想象一下以下场景:应用程序已经在使用实时数据; 代码列包含“name-zip_code”形式的代码(是的,我知道它很难看,但它确实发生了),并且您想将其分成两列,“name”和“zip_code”,同时去掉“代码'栏。
在这种情况下,在数据传输到姓名和邮政编码列之前无法删除代码列。
a lot of times, migrations are the best fit and cannot be replaced with a separate script. Imagine the following scenario: the application is already in use with live data; the code column contains a code in the form "name-zip_code" (yeah I know it's ugly, but it happens), and you want to split that into two columns, 'name' and 'zip_code', while getting rid of the 'code' column.
in this case, the code column cannot be removed before the data is transfered to the name and zip code columns.
当我需要修改数据库中的某些数据时,我将创建一个 Rake 任务来运行一些库函数来完成这项工作。 这样,数据操作将是可重复的,并且如果需要,也可以从迁移中运行。
When I need to modify some data in the databse, I will create a Rake task that runs some library function to do the work. This way, the data manipulation will be repeatable and if required, can be run from a migration as well.