aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md7
-rw-r--r--activerecord/activerecord.gemspec5
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb5
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb20
-rw-r--r--activerecord/test/cases/associations/has_many_through_associations_test.rb11
-rw-r--r--activerecord/test/cases/query_cache_test.rb3
6 files changed, 40 insertions, 11 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index d4f4041910..f780029a18 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
+* Previously, when building records using a `has_many :through` association,
+ if the child records were deleted before the parent was saved, they would
+ still be persisted. Now, if child records are deleted before the parent is saved
+ on a `has_many :through` association, the child records will not be persisted.
+
+ *Tobias Kraze*
+
* Merging two relations representing nested joins no longer transforms the joins of
the merged relation into LEFT OUTER JOIN. Example to clarify:
diff --git a/activerecord/activerecord.gemspec b/activerecord/activerecord.gemspec
index 450ec0bba9..a626a1f21b 100644
--- a/activerecord/activerecord.gemspec
+++ b/activerecord/activerecord.gemspec
@@ -21,6 +21,11 @@ Gem::Specification.new do |s|
s.extra_rdoc_files = %w(README.rdoc)
s.rdoc_options.concat ["--main", "README.rdoc"]
+ s.metadata = {
+ "source_code_uri" => "https://github.com/rails/rails/tree/v#{version}/activerecord",
+ "changelog_uri" => "https://github.com/rails/rails/blob/v#{version}/activerecord/CHANGELOG.md"
+ }
+
s.add_dependency "activesupport", version
s.add_dependency "activemodel", version
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb
index 53ffb3b68d..2fd20b4368 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -109,6 +109,11 @@ module ActiveRecord
record
end
+ def remove_records(existing_records, records, method)
+ super
+ delete_through_records(records)
+ end
+
def target_reflection_has_associated_record?
!(through_reflection.belongs_to? && owner[through_reflection.foreign_key].blank?)
end
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 169e1a5449..b02653bbcc 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -745,35 +745,35 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
firm = Firm.all.merge!(order: "id").first
collection = firm.clients
- original_object_id = collection.first.object_id
- assert_equal original_object_id, collection.first.object_id, "Expected second call to #first to cache the same object"
+ original_object = collection.first
+ assert_same original_object, collection.first, "Expected second call to #first to cache the same object"
# It should return a different object, since the association has been reloaded
- assert_not_equal original_object_id, firm.clients.first.object_id, "Expected #first to return a new object"
+ assert_not_same original_object, firm.clients.first, "Expected #first to return a new object"
end
def test_find_first_after_reset
firm = Firm.all.merge!(order: "id").first
collection = firm.clients
- original_object_id = collection.first.object_id
- assert_equal original_object_id, collection.first.object_id, "Expected second call to #first to cache the same object"
+ original_object = collection.first
+ assert_same original_object, collection.first, "Expected second call to #first to cache the same object"
collection.reset
# It should return a different object, since the association has been reloaded
- assert_not_equal original_object_id, collection.first.object_id, "Expected #first after #reload to return a new object"
+ assert_not_same original_object, collection.first, "Expected #first after #reset to return a new object"
end
def test_find_first_after_reload
firm = Firm.all.merge!(order: "id").first
collection = firm.clients
- original_object_id = collection.first.object_id
- assert_equal original_object_id, collection.first.object_id, "Expected second call to #first to cache the same object"
- collection.reset
+ original_object = collection.first
+ assert_same original_object, collection.first, "Expected second call to #first to cache the same object"
+ collection.reload
# It should return a different object, since the association has been reloaded
- assert_not_equal original_object_id, collection.first.object_id, "Expected #first after #reload to return a new object"
+ assert_not_same original_object, collection.first, "Expected #first after #reload to return a new object"
end
def test_find_all_with_include_and_conditions
diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb
index 9156f6d57a..1c2138a3d0 100644
--- a/activerecord/test/cases/associations/has_many_through_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -319,6 +319,17 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
assert_includes post.single_people, person
end
+ def test_build_then_remove_then_save
+ post = posts(:thinking)
+ post.people.build(first_name: "Bob")
+ ted = post.people.build(first_name: "Ted")
+ post.people.delete(ted)
+ post.save!
+ post.reload
+
+ assert_equal ["Bob"], post.people.collect(&:first_name)
+ end
+
def test_both_parent_ids_set_when_saving_new
post = Post.new(title: "Hello", body: "world")
person = Person.new(first_name: "Sean")
diff --git a/activerecord/test/cases/query_cache_test.rb b/activerecord/test/cases/query_cache_test.rb
index 9b741545d7..c2d40f2940 100644
--- a/activerecord/test/cases/query_cache_test.rb
+++ b/activerecord/test/cases/query_cache_test.rb
@@ -42,7 +42,8 @@ class QueryCacheTest < ActiveRecord::TestCase
mw = middleware { |env|
Task.find 1
Task.find 1
- assert_equal 1, ActiveRecord::Base.connection.query_cache.length
+ query_cache = ActiveRecord::Base.connection.query_cache
+ assert_equal 1, query_cache.length, query_cache.keys
raise "lol borked"
}
assert_raises(RuntimeError) { mw.call({}) }