Closed moyashipan closed 3 years ago
Hi @moyashipan, thank you for submitting this bug report! I'm curious are find_sti_class
and sti_name
private methods? Does this work using inheritance_column
?
Hi @tlynam, thank you for your reply.
I'm curious are find_sti_class and sti_name private methods?
Only find_sti_class
is private method.
Overriding of find_sti_class
is a way known for a long time.
https://stackoverflow.com/questions/23293177/rails-sti-how-to-change-mapping-between-class-name-value-of-the-type-column
Yep, I missed it. My code must be like this:
def version_reification_class(version, attrs)
klass = version.item_type.constantize
inher_col_value = attrs[klass.inheritance_column]
- inher_col_value.blank? ? klass : klass.find_sti_class(inher_col_value)
+ inher_col_value.blank? ? klass : klass.send(:find_sti_class, inher_col_value)
end
However, In Rails 6.1, sti_class_for
will be added and we will be able to call the public method to get STI Class.
https://github.com/rails/rails/pull/37500/commits/3e2822684f90bfc03f35e0a172a60175a62102d7
Does this work using inheritance_column?
Yes. My new suggested code works fine with or without overriding find_sti_code
and inheritance_column
.
Thank you @moyashipan for the explanation and updated script!
So you're proposing we implement this fix? I'm curious why you chose klass.send(:find_sti_class, inher_col_value)
compared to klass.find_sti_class(inher_col_value)
?
def version_reification_class(version, attrs)
klass = version.item_type.constantize
inher_col_value = attrs[klass.inheritance_column]
inher_col_value.blank? ? klass : klass.send(:find_sti_class, inher_col_value)
end
This issue has been automatically marked as stale due to inactivity. The resources of our volunteers are limited. Bug reports must provide a script that reproduces the bug, using our template. Feature suggestions must include a promise to build the feature yourself. Thank you for all your contributions.
paper_trail
gemhttps://github.com/paper-trail-gem/paper_trail/blob/294b7188e95c288b212d997b3a6b4e3759dadf43/lib/paper_trail/reifier.rb#L122-L127
I think the above code should be below.
And then
See also: https://github.com/rails/rails/blob/90216abe121aa9da12e9338722f8f25d252dcf75/activerecord/lib/active_record/inheritance.rb#L246-L250