How do I force ActiveRecord to reload a class?

后端 未结 3 442
轻奢々
轻奢々 2021-01-31 07:25

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 ActiveRecor

相关标签:
3条回答
  • 2021-01-31 07:40

    I always used new models in migrations

        MyBlog < ActiveRecord::Base
          set_table_name 'blogs'
        end
    
        def self.up
          MyBlog.all.each do |blog|
            update_some_blog_attributes_to_match_new_schema
          end
        end
    

    But Blog.reset_column_information is more convenient.

    0 讨论(0)
  • 2021-01-31 07:42

    Create new instances:


    Old_blogs = Blog.all
    

    # change/modify db table in here

    New_blogs = Blog.all # this should be reloaded or you could use the .reload on this
    

    # change information, load old into new

    ex.

    Old_blogs.each do |blog|
      New_blogs.find(blog.id).title = blog.title
    end
    
    0 讨论(0)
  • 2021-01-31 07:49

    The answer is yes!

    Blog.reset_column_information
    
    0 讨论(0)
提交回复
热议问题