[LRUG] Binding the creation of 2+ objects together
Tom Ward
tom at popdog.net
Thu Jul 31 09:49:04 PDT 2008
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....
Tom
More information about the Chat
mailing list