Call Model.reset_column_information
for each migrations.
# 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
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
Add this line to your application's Gemfile:
gem "activerecord-always_reset_column_information"
The gem is available as open source under the terms of the MIT License.