Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Don't fail when trying to remove an unsaved object #94

Closed
wants to merge 1 commit into from

5 participants

@dmathieu

When trying to destroy an unsaved object (this happens in some of our tests), acts as paranoid tries to remove the object, and calls it's id for that with

Array(self.class.primary_key), Array(self.id)].transpose

As self.id is nil, transpose can't work because the array is missing an entry.
And we don't need to run any sql query to remove an object if nothing has been saved into the database yet.

@avit

Confirmed, I'm seeing this problem here too. This pull request fixes it. :thumbsup:

Problem appears as:

Exception: IndexError: element size differs (0 should be 1)
--
0: /var/lib/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:206:in `rescue in transaction'
1: /var/lib/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:180:in `transaction'
2: /var/lib/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/transactions.rb:208:in `transaction'
3: /var/lib/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/associations/has_one_association.rb:14:in `replace'
4: /var/lib/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/associations/singular_association.rb:17:in `writer'
@kuboon

how about destroy! method?

@dmathieu

@kuboon: the destroy! method has been implemented only in rails 4. This pull request has no meaning to adding the support of rails master.

@chuckg chuckg closed this pull request from a commit
@chuckg chuckg Merge branch 'dmathieu-remove_unsaved_objects' into rails3.2
Fixes #94 error when destroying an unpersisted object.
aa2f9c4
@chuckg chuckg closed this in aa2f9c4
@avit avit referenced this pull request
Closed

Release 0.4.2 with fixes #112

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 22, 2013
  1. @dmathieu
This page is out of date. Refresh to see the latest.
Showing with 5 additions and 1 deletion.
  1. +1 −1  lib/acts_as_paranoid/core.rb
  2. +4 −0 test/test_core.rb
View
2  lib/acts_as_paranoid/core.rb
@@ -106,7 +106,7 @@ def destroy
with_transaction_returning_status do
run_callbacks :destroy do
# Handle composite keys, otherwise we would just use `self.class.primary_key.to_sym => self.id`.
- self.class.delete_all(Hash[[Array(self.class.primary_key), Array(self.id)].transpose])
+ self.class.delete_all(Hash[[Array(self.class.primary_key), Array(self.id)].transpose]) if persisted?
self.paranoid_value = self.class.delete_now_value
self
end
View
4 test/test_core.rb
@@ -63,6 +63,10 @@ def test_real_removal
assert_empty ParanoidTime.with_deleted.all
end
+ def test_removal_not_persisted
+ assert ParanoidTime.new.destroy
+ end
+
def test_recovery
assert_equal 3, ParanoidBoolean.count
ParanoidBoolean.first.destroy
Something went wrong with that request. Please try again.