Do transactions fail with polymorphic has_one relationships?

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
@artisan_quote_input.save!, 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
successfully.

Supposedly, according to AWDWR, 2nd. ed. (p. 386), there should be an
implicit transaction around the @a.save! 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
@artisan_quote_input.save!
end

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?

Thanks,
Wes

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs