diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-01-13 15:04:23 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-01-13 15:07:14 +0100 |
commit | e95031f55dd54945abebe6e9f8a12e428ada4146 (patch) | |
tree | 78980ad99b642e51df1b0fdeb366f53044ef1740 | |
parent | ee4b5f1b8aedc59d94e72e1f0689de48e3b477bb (diff) | |
download | rails-e95031f55dd54945abebe6e9f8a12e428ada4146.tar.gz rails-e95031f55dd54945abebe6e9f8a12e428ada4146.tar.bz2 rails-e95031f55dd54945abebe6e9f8a12e428ada4146.zip |
fix bug in becomes! when changing from base to subclass. Closes #13272.
-rw-r--r-- | activerecord/CHANGELOG.md | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/persistence.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/inheritance_test.rb | 11 |
3 files changed, 22 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index d9f8ee7097..f57158f38e 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* Fix bug in `becomes!` when changing from the base model to a STI sub-class. + + Fixes #13272. + + *the-web-dev*, *Yves Senn* + * Currently Active Record can be configured via the environment variable `DATABASE_URL` or by manually injecting a hash of values which is what Rails does, reading in `database.yml` and setting Active Record appropriately. Active Record diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 4669c072a1..460fbdb3f8 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -196,7 +196,11 @@ module ActiveRecord # share the same set of attributes. def becomes!(klass) became = becomes(klass) - became.public_send("#{klass.inheritance_column}=", klass.sti_name) unless self.class.descends_from_active_record? + sti_type = nil + if !klass.descends_from_active_record? + sti_type = klass.sti_name + end + became.public_send("#{klass.inheritance_column}=", sti_type) became end diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index cb0d374ef7..7fd7d42354 100644 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -128,6 +128,17 @@ class InheritanceTest < ActiveRecord::TestCase assert_kind_of Cabbage, cabbage end + def test_alt_becomes_bang_resets_inheritance_type_column + vegetable = Vegetable.create!(name: "Red Pepper") + assert_nil vegetable.custom_type + + cabbage = vegetable.becomes!(Cabbage) + assert_equal "Cabbage", cabbage.custom_type + + vegetable = cabbage.becomes!(Vegetable) + assert_nil cabbage.custom_type + end + def test_inheritance_find_all companies = Company.all.merge!(:order => 'id').to_a assert_kind_of Firm, companies[0], "37signals should be a firm" |