instance method ignored_columns=

Ruby on Rails 7.1.6

Since v5.2.8.1

Available in: 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

ignored_columns=(columns)

Sets the columns names the model should ignore. Ignored columns won’t have attribute accessors defined, and won’t be referenced in SQL queries.

A common usage pattern for this method is to ensure all references to an attribute have been removed and deployed, before a migration to drop the column from the database has been deployed and run. Using this two step approach to dropping columns ensures there is no code that raises errors due to having a cached schema in memory at the time the schema migration is run.

For example, given a model where you want to drop the “category” attribute, first mark it as ignored:

class Project < ActiveRecord::Base
  # schema:
  #   id         :bigint
  #   name       :string, limit: 255
  #   category   :string, limit: 255

  self.ignored_columns += [:category]
end

The schema still contains “category”, but now the model omits it, so any meta-driven code or schema caching will not attempt to use the column:

Project.columns_hash["category"] => nil

You will get an error if accessing that attribute directly, so ensure all usages of the column are removed (automated tests can help you find any usages).

user = Project.create!(name: "First Project")
user.category # => raises NoMethodError

Parameters

columns req
Source
# File activerecord/lib/active_record/model_schema.rb, line 362
      def ignored_columns=(columns)
        reload_schema_from_cache
        @ignored_columns = columns.map(&:to_s).freeze
      end

Defined in activerecord/lib/active_record/model_schema.rb line 362 · 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