instance method
ignored_columns=
Ruby on Rails 6.0.6
Since v5.2.8.1Signature
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
-
columnsreq
Source
# File activerecord/lib/active_record/model_schema.rb, line 319
def ignored_columns=(columns)
@ignored_columns = columns.map(&:to_s)
end
Defined in activerecord/lib/active_record/model_schema.rb line 319
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::ModelSchema::ClassMethods