[LRUG] Binding the creation of 2+ objects together

Tom Ward tom at popdog.net
Thu Jul 31 11:02:09 PDT 2008


On Thu, Jul 31, 2008 at 5:49 PM, Tom Ward <tom at popdog.net> wrote:
> On Thu, Jul 31, 2008 at 5:39 PM, Anthony Green <email at acgreen.co.uk> wrote:
>>
>> I thought transactions were usually called within a controller ?
>
> I don't think so.  In almost all circumstances, database transactions
> belong in the model.
>
>> What I'm looking for is a solution defined as part of the association
>> declaration thereby enforcing the business rules the objects have.
>>
>> ..or similar
>
> I think if you do something like:
>
> b = Book.new(:title => 'Catch 22')
> b.author.build(:name => 'Joseph Heller')
> b.save
>
> b.save should save the author and the book, both within a transaction
> (so if one fails, they will both fail)
>
> CAUTION: I've not actually tried this....

I've tried it now, and contrary to Matthew's assertion, it doesn't
work.  It works in the case where a database constraint causes the
author not to save, but not where a validation failure causes the
author not to save.

http://gist.github.com/3490

Tom



More information about the Chat mailing list