Permalink
Browse files

Merge pull request #16403 from Peeja/baseclass-becomes-subclass-for-4-1

Backport "Fix Baseclass becomes! subclass." for 4.1

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
2 parents 1211159 + 257d65e commit 127749c2bf60edbd8d6afc136893c3fd342e9d32 @senny senny committed Aug 15, 2014
@@ -1,3 +1,12 @@
+* 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*
+
* Fix regression on after_commit that didnt fire when having nested transactions.
Fixes #16425
@@ -72,7 +72,14 @@ def insert(values) # :nodoc:
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,
@@ -339,6 +339,15 @@ def test_preserve_original_sti_type
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

0 comments on commit 127749c

Please sign in to comment.