Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix belongs_to polymorphic associations with "empty string" type #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

santir489
Copy link

@santir489 santir489 commented Jan 18, 2024

Hi folks!
I have encountered the following issue while using this gem.

Issue:
When a model has a belong_to, polymorphic and optional association, and the model has <polymorphic_name>_type = '' set, a NameError exception is raised when saving the model.

Example

class Comment < ActiveRecord::Base
  belongs_to :post, polymorphic: true, optional: true

  has_paper_trail
end

When executing the following line, it fails with NameError (wrong constant name )

Comment.new(post_type: '').save!

This error raises in these lines of code

foreign_type = @record.send(assoc.foreign_type). # returns '' (empty string)
# and then
foreign_type.constantize # raises the error

You can use this script to reproduce the issue.

IMO having polymorphic_type ='' is allowed/supported by Rails, and this gem should not break when trying to versioning an association with this characteristic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant