aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEdo Balvers <i@edo.me>2014-05-04 15:29:17 +0200
committerEdo Balvers <i@edo.me>2014-05-13 19:30:29 +0200
commit71c5d565b5b660c459966dabb99e491cbc7c2c99 (patch)
tree7f30b0eb27c6bd296524913e083c1d2b5bcc7d81
parent977d36af8f84f467b6e4d21115cd4f84e50cfcb3 (diff)
downloadrails-71c5d565b5b660c459966dabb99e491cbc7c2c99.tar.gz
rails-71c5d565b5b660c459966dabb99e491cbc7c2c99.tar.bz2
rails-71c5d565b5b660c459966dabb99e491cbc7c2c99.zip
Fix Baseclass becomes! subclass.
-rw-r--r--activerecord/CHANGELOG.md9
-rw-r--r--activerecord/lib/active_record/relation.rb9
-rw-r--r--activerecord/test/cases/persistence_test.rb9
3 files changed, 26 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index f6cd5efb45..ba1a6ae0fd 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -57,6 +57,15 @@
*Paul B.*
+* Baseclass becomes! subclass.
+
+ Before this change, a record which changed its STI type, could not be found when updated.
+ Setting update_record to the base class, ensures the record can be found.
+
+ Fixes #14785
+
+ *Matthew Draper*, *Earl St Sauver*, *Edo Balvers*
+
* Add support for module-level `table_name_suffix` in models.
This makes `table_name_suffix` work the same way as `table_name_prefix` when
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 24b33ab0a8..0458018951 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -73,7 +73,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 5d963098fb..462e5695e0 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