No commit activity in last 3 years
No release in over 3 years
Call Model.reset_column_information for each migrations
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0

Runtime

 Project Readme

Activerecord::AlwaysResetColumnInformation

What's this

Call Model.reset_column_information for each migrations.

Why reset_column_information is needed?

# 001_create_users.rb
class CreateUsers < ActiveRecord::Migration[5.1]
  def up
    create_table :users do |t|
      t.string :name
    end
    p User.first # 1
  end
end

# 002_add_age_to_users.rb
class AddAgeToUsers < ActiveRecord::Migration[5.1]
  def up
    add_column :users, :age, :integer  # 2
    User.create(name: "Taro", age: 16) # 3
  end
end

1 Touch User model and columns are cached.
2 Add column to users table. but User's column cache is still keeping.
3 Create user with new column but raise ActiveModel::UnknownAttributeError: unknown attribute 'age' for User.

You can change 002_add_age_to_users.rb to ensure that creating user.

 class AddAgeToUsers < ActiveRecord::Migration[5.1]
   def up
     add_column :users, :age, :integer
+    User.reset_column_information
     User.create(name: "Taro", age: 16)
   end
 end

But Reset after touch in 001_create_users.rb is better approach.

 class CreateUsers < ActiveRecord::Migration[5.1]
   def up
     create_table :users do |t|
       t.string :name
     end
     p User.first
+    User.reset_column_information
   end
 end

What does this gem do?

Reset all models for each migrations.

Implementation is only this.

module Activerecord::AlwaysResetColumnInformation::Migration
  def exec_migration(conn, direction)
    super
    ActiveRecord::Base.descendants.each(&:reset_column_information)
  end
end

ActiveRecord::Migration.prepend Activerecord::AlwaysResetColumnInformation::Migration

Usage

Add this line to your application's Gemfile:

gem "activerecord-always_reset_column_information"

License

The gem is available as open source under the terms of the MIT License.