Ruby

Ruby

Made by DeepSource

Ignored column accessed from ActiveRecord model RB-E1010

Bug risk
Major

The ignored_columns attribute allows the exclusion of certain columns when working with ActiveRecord models in Rails. However, accessing an ignored column from an ActiveRecord model wail raise a NoMethodError.

Columns are added to ignored_columns when they need to be deprecated in the near future, this is the first step recommended before removing a column from a model. Hence, accessing ignored_columns should be avoided.

Bad practice

class User < ApplicationRecord
    self.ignored_columns = [:email]
end

User.find_by(email: '[email protected]') # This will raise a NoMethodError

Recommended

class User < ApplicationRecord
    self.ignored_columns = [:email]
end

User.find_by(name: 'John Appleseed') # This will not raise any error

References

  1. ActiveRecord API documentation