I’m not entirely sure that’s the probem, as the code I’m using is the
same as the Quote code. It’s all very useful nontheless as it’s giving
more more information to trace the problem with. Here is the code from
the quote controller which works perfectly:
Chris T wrote:
Rig has_one :eventdetail
(i.e. rig is a parent of eventdetail), and when you save a parent the
child isn’t automatically saved (although the converse is true).
The other problem would seem to be that the new eventdetail (which
belongs_to @rig) has_many :rig_items, which are also new records, and
which also won’t be automatically be saved when @rig is saved.
In my working quote controller, the child Rig_items are associated to
unsaved Eventdetail, then the unsaved Eventdetail is assigned to the
unsaved Quote. When the Quote is saved, the Eventdetail and Rig_items
are saved in one go, automatically transactioned by active record. You
can see this in my development log:
Comparing this with creating a rig and you can see all kinds of mess
The same code works as soon as you remove the association with Quote
from the Rig and Quote models, as per this log:
So from this info, it seems that for some reason active record is trying
to do something unexpected with the associated Quote when saving the
What I’m expecting to happen is that when I do:
@rig.quote = @quote
nothing is saved. When I do:
then the association (@rig.quote_id) is saved, but not the quote (as
it’s an existing record).
I would have only expected problems if I’d done
@quote.rigs << @rig
Which would try and save the Rig straight away, as I’d have to validate
it first. In fact replacing @rig.quote = @quote with @quote.rigs <<
@rig still causes the same error and log messages.
So again I’m quite stuck, but the problem is narrowed down to exactly
what should happen / actually happens when you try and save a new record
with an existing record associated to it.