如何强制ActiveRecord重新加载类? [英] How do I force ActiveRecord to reload a class?

查看:92
本文介绍了如何强制ActiveRecord重新加载类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一堆迁移,其中一些是标准的创建表"或修改表"迁移,其中一些是修改数据.我正在使用实际的ActiveRecord模型来修改数据,例如:

I'm creating a bunch of migrations, some of which are standard "create table" or "modify table" migrations, and some of which modify data. I'm using my actual ActiveRecord models to modify the data, a la:

Blog.all.each do |blog|
  update_some_blog_attributes_to_match_new_schema
end

问题在于,如果我加载Blog类,然后修改表,然后再次使用Blog类,则模型具有旧的表定义,并且无法保存到新表.有没有一种方法可以重新加载类及其属性定义,以便我可以重用它们?

The problem is that if I load the Blog class, then modify the table, then use the Blog class again, the models have the old table definitions, and cannot save to the new table. Is there a way to reload the classes and their attribute definitions so I can reuse them?

推荐答案

答案是肯定的!

Blog.reset_column_information

这篇关于如何强制ActiveRecord重新加载类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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