diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-06-06 08:55:25 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-06-06 08:56:29 +0200 |
commit | 01777079cd7a137dd26537a32c25dea306e96e5a (patch) | |
tree | 4b7053569b19df4c7e432997e2253e0ad9afadd7 /activerecord | |
parent | aa54c91a151b47656320b511b30f546180202458 (diff) | |
parent | 71c5d565b5b660c459966dabb99e491cbc7c2c99 (diff) | |
download | rails-01777079cd7a137dd26537a32c25dea306e96e5a.tar.gz rails-01777079cd7a137dd26537a32c25dea306e96e5a.tar.bz2 rails-01777079cd7a137dd26537a32c25dea306e96e5a.zip |
Merge pull request #14971 from versioncontrol/#14785
Baseclass becomes! subclass
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 9 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 9 | ||||
-rw-r--r-- | activerecord/test/cases/persistence_test.rb | 9 |
3 files changed, 26 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 6491f32f8f..7acb92620c 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,12 @@ +* Baseclass becomes! subclass. + + Before this change, a record which changed its STI type, could not be + updated. + + Fixes #14785. + + *Matthew Draper*, *Earl St Sauver*, *Edo Balvers* + * Remove deprecated `ActiveRecord::Migrator.proper_table_name`. Use the `proper_table_name` instance method on `ActiveRecord::Migration` instead. diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index d92ff781ee..cef40be7ce 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -74,7 +74,14 @@ module ActiveRecord def _update_record(values, id, id_was) # :nodoc: substitutes, binds = substitute_values values - um = @klass.unscoped.where(@klass.arel_table[@klass.primary_key].eq(id_was || id)).arel.compile_update(substitutes, @klass.primary_key) + + scope = @klass.unscoped + + if @klass.finder_needs_type_condition? + scope.unscope!(where: @klass.inheritance_column) + end + + um = scope.where(@klass.arel_table[@klass.primary_key].eq(id_was || id)).arel.compile_update(substitutes, @klass.primary_key) @klass.connection.update( um, diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index bc5ccd0fe9..16f481c289 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -333,6 +333,15 @@ class PersistenceTest < ActiveRecord::TestCase assert_equal "Reply", topic.type end + def test_update_sti_subclass_type + assert_instance_of Topic, topics(:first) + + reply = topics(:first).becomes!(Reply) + assert_instance_of Reply, reply + reply.save! + assert_instance_of Reply, Reply.find(reply.id) + end + def test_update_after_create klass = Class.new(Topic) do def self.name; 'Topic'; end |