Join GitHub today
#reset_column_information (from active_record/base) does not work properly with inheritance #22057
reset_column_information only works on the Model that calls the method, and not on all models that inherit from it. This seems like bad behavior, because if you are going out of your way to call #reset_column_information--then you want all models that changed to be up to date.
I created a small app that displays the problem here: https://github.com/common-nighthawk/rails-reset_column_information-bug
In summary, say I have two models--
Both have 2 columns: id and type.
Then I run this migration--
The Animal will have greeting 'im an animal' and the Bird will have greeting 'nil'.
I can get around this by calling #reset_column_information on all Models explicitly. But this feel clunky, because all models that inherit from the parent have changing columns--so I don't see why the method call should not update all.
File - activerecord-4.2.3/lib/active_record/attributes.rb
def reset_column_information super clear_caches_calculated_from_columns # Change 1 descendants.map(&:clear_caches_calculated_from_columns) end # Change 2 -(private to public) public def clear_caches_calculated_from_columns # no change in body end
I have tested this with attached code and is working fine.
I don't know if this approach is better or worse, but I discovered this fix--
calling #reset_column_information recursively on the subclasses, with:
advantages would be: not needing to expose a method to the public api (keep private)
would love thoughts from someone who knows better :) . happy to submit a PR.
This approach even clicked me.
This should get called only once for whole hierarchy since in STI, table is shared by all the classes in hierarchy. Hence clearing table cache should be one time activity.
Below code should get called for all the subclasses
def clear_caches_calculated_from_columns @attributes_builder = nil @column_names = nil @column_types = nil @columns = nil @columns_hash = nil @content_columns = nil @default_attributes = nil @persistable_attribute_names = nil end
@sgrif: thanks for fixing this. always cool when opening an issue on a big open source project actually results in a quick patch.
i didn't realize #reset_column_information had recently been split to call the private method #reload_schema_from_cache.
change makes sense to me. thanks again. also--fan of the bike shed.