在 Rails 迁移中更改对象
我想将当前拥有的belongs_to关系迁移到has_and_belongs_to_many关系。理想情况下,我想在我构建的迁移中执行此操作。
当我添加这个新迁移(即这两个项目的联接 ID)时,我可以随后对该迁移中的模型进行更改吗?
#migration code goes here to add the new item field
Item.find(:all).each do |item|
specific changes to item to account for the new relationship
end
或者说这不是我在 rake 环境中可以做的事情吗?我想将对象迁移到使用新的 HABTM 系统,我是否必须在实际代码本身中检查这一点,并在启动应用程序实例之前将其作为单独的 rake 任务运行?
I'd like to migrate a belongs_to relationship that I currently have to a has_and_belongs_to_many relationship. Ideally I'd like to do this in the migration that I've built.
When I add this new migration which is the join IDs for these two items, can I then afterwards make changes to the model in that Migration?
#migration code goes here to add the new item field
Item.find(:all).each do |item|
specific changes to item to account for the new relationship
end
Or is that not something I can do within that rake environment? I'd like to migrate the objects over to using the new HABTM system, will I have to check for that in the actual code itself and run that as a separate rake task before launching the instance of the application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那完全没问题。尽管如果您正在升级并需要进行更改,可能会花费大量时间,但迁移是一个可以接受的地方。
That is perfectly fine. Although it can take a lot of time if you are upgrading and require changes to be made, the migration is an acceptable place to do it.