Created
April 24, 2012 09:14
ActiveRecord#becomes doesn't destroy associations any more
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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