James A. Rosen James A. Rosen - 5 months ago 19
Ruby Question

How do I force ActiveRecord to reload a class?

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


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?

Answer

The answer is yes!

Blog.reset_column_information