Rails模型验证器打破了早期的迁移 [英] Rails model validators break earlier migrations

查看:60
本文介绍了Rails模型验证器打破了早期的迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rails应用程序中进行了一系列迁移,包括以下步骤:

I have a sequence of migrations in a rails app which includes the following steps:

  1. 创建用户"模型的基本版本
  2. 创建此模型的一个实例-我的系统中至少需要有一个初始用户,以便您可以登录并开始使用它
  3. 更新用户"模型以添加新的字段/列.

现在,我在此新字段/列上使用"validates_inclusion_of".这在我最初的开发机器上运行良好,该机器已经有一个应用了这些迁移的数据库.但是,如果我转到一台新机器并运行所有迁移,则步骤2将失败,因为validates_inclusion_of失败,因为尚未将迁移3中的字段添加到模型类中.

Now I'm using "validates_inclusion_of" on this new field/column. This worked fine on my initial development machine, which already had a database with these migrations applied. However, if I go to a fresh machine and run all the migrations, step 2 fails, because validates_inclusion_of fails, because the field from migration 3 hasn't been added to the model class yet.

作为一种解决方法,我可以注释掉"validates _..."行,运行迁移,然后取消注释,但这不是很好.

As a workaround, I can comment out the "validates_..." line, run the migrations, and uncomment it, but that's not nice.

最好将我的迁移重新排序,以便在所有列都添加完之后,用户创建(第2步)排在最后.

Better would be to re-order my migrations so the user creation (step 2) comes last, after all columns have been added.

虽然我是Rails新手,所以我想问一下处理这种情况的首选方法是:)

I'm a rails newbie though, so I thought I'd ask what the preferred way to handle this situation is :)

推荐答案

避免此问题的最简单方法是在第二台计算机上使用rake db:schema:load,而不是db:migrate. D:S:L使用schema.rb加载架构的最新版本,而不是从头开始将其迁移.

The easiest way to avoid this issue is to use rake db:schema:load on the second machine, instead of db:migrate. D:S:L uses schema.rb to load the most current version of your schema, as opposed to migrating it up form scratch.

如果在部署到生产计算机(在保留数据很重要的地方)时遇到此问题,则可能必须将迁移合并到一个文件中而不会产生冲突.

If you run into this issue when deploying to a production machine (where preserving data is important), you'll probably have to consolidate your migrations into a single file without conflicts.

这篇关于Rails模型验证器打破了早期的迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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