rails_version = Gem::Version.new(Rails.version)
if rails_version < Gem::Version.new("3")
  raise "This fix is written for Rails 3 and not tested with Rails 2 yet"
elsif rails_version > Gem::Version.new("4")
  raise "Remove this file. Rails 4 contains this fix."
elsif rails_version > Gem::Version.new("3.2.19")
  warn "  #{__FILE__} Warning:\n  This fix should have been implemented in the next Rails LTS Release!\n  In case it is, remove this file."
end

ActiveRecord::Persistence.class_eval do

  def becomes(klass)
    became = klass.new
    became.instance_variable_set("@attributes", @attributes)
    became.instance_variable_set("@attributes_cache", @attributes_cache)

    # backport from rails 4
    changed_attributes = @changed_attributes if defined?(@changed_attributes)
    became.instance_variable_set("@changed_attributes", changed_attributes || {})

    became.instance_variable_set("@new_record", new_record?)
    became.instance_variable_set("@destroyed", destroyed?)
    became.instance_variable_set("@errors", errors)
    became.send("#{klass.inheritance_column}=", klass.name) unless self.class.descends_from_active_record?
    became
  end

end