You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The issue occurs because setting commentable to a User results in commentable_type being nil. The validation passes since this condition is not met.
It's inconsistent with the ActiveRecord's behaviour, where assigning a nil association triggers a validation error.
Proposed Fix
To align the validation logic with ActiveRecord, I suggest this:
validatedot=send(foreign_type)ift.nil?errors.add(foreign_type,"must exist")elsif !mapping.values.include?(t)errors.add(foreign_type,"is not included in the mapping")endend
The changes ensures a nilcommentable_type triggers a validation error.
Versions:
polymorphic_integer_type: 3.3.0
ruby: 3.1.1
The text was updated successfully, but these errors were encountered:
Problem:
When using
polymorphic_integer_type
, associating aComment
with a model that is not in the polymorphic mappings does not trigger a validation error.For example, given this model:
If a
Comment
is associated with aUser
(which is not part of the mapping), callingvalid?
incorrectly returnstrue
:This is unexpected because
User
is not in the allowed mappings.Expected Behaviour:
Analysis:
The issue occurs because setting
commentable
to aUser
results incommentable_type
beingnil
. The validation passes since this condition is not met.It's inconsistent with the
ActiveRecord
's behaviour, where assigning anil
association triggers a validation error.Proposed Fix
To align the validation logic with
ActiveRecord
, I suggest this:The changes ensures a
nil
commentable_type
triggers a validation error.Versions:
The text was updated successfully, but these errors were encountered: