# Strange Active::Record behavior, StatementInvalid exception thrown twice?

**URL:** <https://www.ruby-forum.com/t/strange-active-record-behavior-statementinvalid-exception-thrown-twice/174427>\
**Category:** Rails\
**Created:** [August 26, 2009, 6:06pm UTC](https://www.ruby-forum.com/t/strange-active-record-behavior-statementinvalid-exception-thrown-twice/174427 "2009-08-26T18:06:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@anr](https://www.ruby-forum.com/u/anr)\
**Post date:** [August 26, 2009, 6:06pm UTC](https://www.ruby-forum.com/t/strange-active-record-behavior-statementinvalid-exception-thrown-twice/174427/1 "2009-08-26T18:06:10Z")

</div>

Hi,

I’m a bit puzzled with an ActiveRecord::StatementInvalid exception  
that’s apparently being thrown twice.

I’m using Rails 2.3.3 and Postgres, and inside the Feed Model I have  
something like:

has\_many :entries, :dependent =\> :delete\_all

def process\_feed  
…  
process\_feed\_entries

# log saving feed

save! # will fail if an exception is thrown below  
end

def process\_feed\_entries  
…  
begin  
# log saving entry  
entry.save! # can fail because of duplicate key values  
rescue ActiveRecord::StatementInvalid =\> e  
# log exception caught  
# log the error in the db (Log Model)  
end  
end

DB log:

- Saving entry  
BEGIN  
PGError: ERROR: duplicate key value … : INSERT INTO “entries” …  
ROLLBACK
- Exception caught  
BEGIN  
INSERT INTO “logs” …  
COMMIT
- Saving feed  
BEGIN  
UPDATE “feeds” …  
PGError: ERROR: duplicate key value … : INSERT INTO “entries”  
ROLLBACK  
ActiveRecord::StatementInvalid: PGError: ERROR: duplicate key …  
Stack trace

The last exception that terminates execution was (or should have been)  
caught inside the begin/rescue block. Why is it thrown again when the  
feed is saved, if the transaction was rolled back?

Any hints much appreciated!

Thanks,

–  
Adriano

---

<div class="post-metadata">

**Author:** ![anr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@anr](https://www.ruby-forum.com/u/anr)\
**Post date:** [August 26, 2009, 8:29pm UTC](https://www.ruby-forum.com/t/strange-active-record-behavior-statementinvalid-exception-thrown-twice/174427/2 "2009-08-26T20:29:41Z")

</div>

On Aug 26, 11:56 am, Adriano N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_many :entries, :dependent =\> :delete\_all  
> save! # will fail if an exception is thrown below

Nothing like explaining your problem to others 🙂

I figure feed.save tries to save the associated entry and that throws  
the exception again.

I have to delete the offending entry inside the rescue clause.

Thanks,

–  
Adriano

---

<div class="post-metadata">

**Author:** ![anr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@anr](https://www.ruby-forum.com/u/anr)\
**Post date:** [August 26, 2009, 10:39pm UTC](https://www.ruby-forum.com/t/strange-active-record-behavior-statementinvalid-exception-thrown-twice/174427/3 "2009-08-26T22:39:41Z")

</div>

On Aug 26, 3:56 pm, Adriano N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def process\_feed  
> entry.save! # can fail because of duplicate key values  
> Is entry a member of the entries collection ? If so then the docs on  
> associations state

“All unsaved (new\_record? == true) members of the collection are  
automatically saved when the parent is saved.”  
ie when you call feed.save! unsaved association members are saved.

Fred

---

<div class="post-metadata">

**Author:** ![anr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@anr](https://www.ruby-forum.com/u/anr)\
**Post date:** [August 26, 2009, 10:46pm UTC](https://www.ruby-forum.com/t/strange-active-record-behavior-statementinvalid-exception-thrown-twice/174427/4 "2009-08-26T22:46:08Z")

</div>

On Aug 26, 7:39 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Aug 26, 3:56 pm, Adriano N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> Is entry a member of the entries collection ? If so then the docs on  
> associations state

Oops, you beat me to it !

Fred
