Search code examples
ruby-on-railsrubyruby-on-rails-6.1ruby-2.7

DEPRECATION WARNING: Calling `<<` to an ActiveModel::Errors message array in order to add an error is deprecated. in ruby


Ruby - 2.7.3 Rails - 6.1.3.2

DEPRECATION WARNING: Calling << to an ActiveModel::Errors message array in order to add an error is deprecated. Please callActiveModel::Errors#add instead. (called from validate at /var/home/application_name/app/models/contacts.rb:4)

1.class AssociatedProgramValidator < ActiveModel::Validator
2.  def validate(record)
3.    unless record.user.programs.pluck(:name).include? record.program_name
4.      record.errors[:program_id] << "No #{record.program_name} found on user"
5.    end
6.  end
7.end
8.
9.class Contacts < ApplicationRecord
10.
11.  include ActiveModel::Validations
12.end

Please give some suggestion.


Solution

  • I suspect it should be:

    record.errors.add(:program_id, message: "No #{record.program_name} found on user")

    https://api.rubyonrails.org/v6.1.3/classes/ActiveModel/Errors.html#method-i-add