Skip to content
master
Switch branches/tags
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
bin
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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.

About

No description, website, or topics provided.

Resources

License

Packages

No packages published