Easy Rails 问题:模型和迁移
每当我更改 Rails 中的模型时,是否需要重新迁移?有人能告诉我迁移到底有什么作用吗?我正在阅读它,我对 db/migrate 内的内容和 app/models 内的内容之间的区别有些困惑。
例如,如果我在模型中添加 has_one 关系,是否需要重新迁移它?为什么?
Do I need to re-migrate whenever I change the models in Rails? Can someone tell me what migration really does? I am reading it and I am somewhat confused with the difference between the stuff inside db/migrate and the stuff inside app/models.
For example, if I add a has_one realtionship inside my model, do I need to re-migrate this? Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的数据库发生更改,请使用迁移。如果您只是向模型添加方法,则无需进行迁移。
示例:
我们一开始只有名字和姓氏。我们想要将它们存储在数据库中,所以我们有一个迁移:
/app/models
/ human.rb /db/migrate/xxxxx.rb
然后我们结婚了,所以我们想要跟踪
/app/models/ human .rb
/db/migrate/xxxxx.rb
/app/models/offspring.rb
/db/migrate/xxxxx.rb
/app/models
/app/models/ human.rb
If your database changes, use a migration. If you're just adding methods to your model, no need to have a migration.
Example:
We start out and we just have first_name, last_name. We want to store those in the database, so we have a migration that does:
/app/models/human.rb
/db/migrate/xxxxx.rb
Then we get married, so we want to track that
/app/models/human.rb
/db/migrate/xxxxx.rb
/app/models/offspring.rb
/db/migrate/xxxxx.rb
/app/models/human.rb
/app/models/human.rb