Skip to content

Instantly share code, notes, and snippets.

@no-dashes
Created April 24, 2012 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save no-dashes/2478161 to your computer and use it in GitHub Desktop.
Save no-dashes/2478161 to your computer and use it in GitHub Desktop.
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