Forum: Ruby on Rails A has_many :bs; what is the Rails idiom to save @a and in one transaction ?

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.
Daniel W. (Guest)
on 2008-10-23 18:02
(Received via mailing list)
Suppose I have two models:

class A < ActiveRecord::Base
  has_many :bs

class B < ActiveRecord::Base
  belongs_to :a

Now, if I say in a controller
  b1 =[:b])
I can build a new B associated to an A.

If the @ is also new, I need to also do this
  b1.a = @a
but if the @a is not new, then its id is filled in for me.

(1) Why?  Even though there is no to set the b1.a to, there is
an @a object that I can set b1.a to, and I do.  Couldn't build do this
for me so would work in both cases?

Either way, if I now say!
Then not only are the values of @a that may have been updated saved,
the new object b1 is also saved to the database.  Not only that, this
all happens in the same transaction.  Yea!

However, instead if I were to update an existing b as follows:
  b1 =[1].update(params[:b])
and then!
then this time, the updated values of b1 are not saved to the

(2) Why?  Wouldn't it be cleaner to update the associated b values
into the database as well during the save of the @a ?

Further, if I want the @a and the associated to all be sent to
the database in the same transaction, as far as I can tell, I have the
following choices.

In my controller save the @a and in one transaction:
ActiveRecord::Base.transaction do do |b|!

Or perhaps to be more elegant I could push all that into a method
save_it! on class A.

Or I can try to figure out how to get the to save in the same
transaction as the @a.  I suppose I can do that as follows:

class A < ActiveRecord::Base
  before_save do |a|
    bs.each do |b|!

This is a bit better, but -- amazingly -- I have somehow at times
gotten into an infinite loop doing this, though it was a bit more
complex than that.  Even though the documentation says that if I save
on the belongs_to side, the object belonged-to will not be saved, I
had a case where saving the associated objects like this ended up
recursing on itself.  Is this idiom supposed to work and was I
therefore getting an infinite loop because (a) I was not doing it
quite right, or (b) is this idiom not good.

(3) What is the right idiom for this situation, where I want to save
the @a and the associated bs in one transaction?

This topic is locked and can not be replied to.