Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ActiveRecord#becomes doesn't destroy associations any more
# Normally if you do
class A < ActiveRecord::Base
belongs_to :c
end
class B < A
end
b = B.new
b.c = C.new
a = b.becomes(A)
# you find that
a.c == nil
# to fix that, we have an initializer:
class ActiveRecord::Base
def becomes_with_association_cache(klass)
became = becomes_without_association_cache(klass)
became.instance_variable_set("@association_cache", @association_cache)
became
end
alias_method_chain :becomes, :association_cache
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment