Skip to content

Instantly share code, notes, and snippets.

class ImmutableValidator < ActiveModel::EachValidator
def validate_each(record, attribute, _value)
return unless record.persisted?
association_key = if record.attribute_names.include?(attribute.to_s)
attribute
else
record.reflections[attribute].foreign_key
end