Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix deleting from a HABTM join table upon destroying an object of a m…

…odel with optimistic locking enabled. Fixes #5332.
  • Loading branch information...
commit 05e1466e059a12bddd066a9f22ca575d81cfc21d 1 parent 5ed0381
@courtland courtland authored
View
4 activerecord/CHANGELOG.md
@@ -1,5 +1,9 @@
## Rails 3.2.10 (unreleased)
+* Fix deleting from a HABTM join table upon destroying an object of a model with optimistic locking enabled. Fixes #5332.
+
+ *Nick Rogers*
+
* Use query cache/uncache when using ENV["DATABASE_URL"].
Fixes #6951. [Backport #8074]
View
2  activerecord/lib/active_record/locking/optimistic.rb
@@ -102,6 +102,8 @@ def update(attribute_names = @attributes.keys) #:nodoc:
def destroy #:nodoc:
return super unless locking_enabled?
+ destroy_associations
+
if persisted?
table = self.class.arel_table
lock_col = self.class.locking_column
View
12 activerecord/test/cases/locking_test.rb
@@ -20,7 +20,7 @@ class ReadonlyNameShip < Ship
end
class OptimisticLockingTest < ActiveRecord::TestCase
- fixtures :people, :legacy_things, :references, :string_key_objects
+ fixtures :people, :legacy_things, :references, :string_key_objects, :peoples_treasures
def test_non_integer_lock_existing
s1 = StringKeyObject.find("record1")
@@ -267,6 +267,16 @@ def test_original_locking_column
assert_equal "omg", k.original_locking_column
end
end
+
+ def test_removing_has_and_belongs_to_many_associations_upon_destroy
+ p = RichPerson.create!
+ p.treasures.create!
+ assert !p.treasures.empty?
+ p.destroy
+ assert p.treasures.empty?
+ assert RichPerson.connection.select_all("SELECT * FROM peoples_treasures WHERE rich_person_id = 1").empty?
+ end
+
end
class OptimisticLockingWithSchemaChangeTest < ActiveRecord::TestCase
View
3  activerecord/test/fixtures/peoples_treasures.yml
@@ -0,0 +1,3 @@
+michael_diamond:
+ rich_person_id: <%= ActiveRecord::Fixtures.identify(:michael) %>
+ treasure_id: <%= ActiveRecord::Fixtures.identify(:diamond) %>
View
6 activerecord/test/models/person.rb
@@ -86,3 +86,9 @@ class TightPerson < ActiveRecord::Base
end
class TightDescendant < TightPerson; end
+
+class RichPerson < ActiveRecord::Base
+ self.table_name = 'people'
+
+ has_and_belongs_to_many :treasures, :join_table => 'peoples_treasures'
+end
View
5 activerecord/test/schema/schema.rb
@@ -480,6 +480,11 @@ def create_table(*args, &block)
t.references :best_friend_of
t.timestamps
end
+
+ create_table :peoples_treasures, :id => false, :force => true do |t|
+ t.column :rich_person_id, :integer
+ t.column :treasure_id, :integer
+ end
create_table :pets, :primary_key => :pet_id ,:force => true do |t|
t.string :name
Please sign in to comment.
Something went wrong with that request. Please try again.