'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 1:[1]
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
Solution 2:[2]
In testing, it looks like the actual equivalent is
record.errors.add(:program_id, "No #{record.program_name} found on user")
I suspect the use of "message" instead of using it as an error type might be a better idea, but your code may not handle the difference without adjustment.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | stolarz |
| Solution 2 |
