Forum: Ruby on Rails Do transactions fail with polymorphic has_one relationships?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2007-01-19 22:25
I have an AR::Base descendant with a complex object graph beneath it,
like so:

ArtisanQuoteInput has_one QuoteInput and then QuoteInput has several
objects attached to it.  Note that the relationship between
ArtisanQuoteInput and QuoteInput is polymorphic, so the relationship in
A is written

  has_one :quote_input, :as => :program_quote_input

If @artisan_quote_input is an instance of ArtisanQuoteInput and I call!, and just one of its attributes is invalid
(meaning that @artisan_quote_input fails validation), what I see is that
the quote_input object and all of its children save to the database

Supposedly, according to AWDWR, 2nd. ed. (p. 386), there should be an
implicit transaction around the! call.  But I'm not seeing this.
I'd expect that none of the child data would be saved.

So I put an explicit transaction around this call, like so:

ArtisanQuoteInput.connection.transaction do!

The same behavior occurs, e.g. an incomplete rollback.

The only thing that seems out of the ordinary is the polymorphic
relationship between ArtisanQuoteInput and QuoteInput, but frankly, I
don't really see why that should matter.

So am I misunderstanding the workings of the transaction method in Rails
or is there something about polymorphic is_one relationships that makes
transactions not work correctly?

This topic is locked and can not be replied to.