instance method reset_column_information

Ruby on Rails 4.0.13

Since v3.2.22.5

Available in: v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

reset_column_information()

Resets all the cached information about columns, which will cause them to be reloaded on the next request.

The most common usage pattern for this method is probably in a migration, when just after creating a table you want to populate it with some default values, eg:

class CreateJobLevels < ActiveRecord::Migration
  def up
    create_table :job_levels do |t|
      t.integer :id
      t.string :name

      t.timestamps
    end

    JobLevel.reset_column_information
    %w{assistant executive manager director}.each do |type|
      JobLevel.create(name: type)
    end
  end

  def down
    drop_table :job_levels
  end
end
Source
# File activerecord/lib/active_record/model_schema.rb, line 302
      def reset_column_information
        connection.clear_cache!
        undefine_attribute_methods
        connection.schema_cache.clear_table_cache!(table_name) if table_exists?

        @arel_engine             = nil
        @column_defaults         = nil
        @column_names            = nil
        @columns                 = nil
        @columns_hash            = nil
        @column_types            = nil
        @content_columns         = nil
        @dynamic_methods_hash    = nil
        @inheritance_column      = nil unless defined?(@explicit_inheritance_column) && @explicit_inheritance_column
        @relation                = nil
        @serialized_column_names = nil
        @time_zone_column_names  = nil
        @cached_time_zone        = nil
      end

Defined in activerecord/lib/active_record/model_schema.rb line 302 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::ModelSchema::ClassMethods

Type at least 2 characters to search.

↑↓ navigate · open · esc close