Adding ActiveRecord objects to the session


#1

Hi all,

I am creating an AJAX quoting/puchasing system and I am having a bit of
unexpected behavior when adding ActiveRecord objects to the session.
When I add the ActiveRecord object (in this case QuotePart) to the
session it automatically saves the object that was added to the session
to the DB. And the oddest part is that it automatically fills in the
foreign key field quote_id, even though it is set to 0 at the time it is
saved to the session.

Here is the offending code, not the debugging statements; the first
stament returns " - 0 - new" and the second statement returns “123 - 4 -
DB” where 123 is whatever id was created and 4, as far as I can tell, is
the id of the last Quote object that was instanciated (many method calls
ago).

Controller:
def add_part
part = Part.find(@params[‘id’].split(’_’)[1])
if part
qp = QuotePart.new_from_part(part, part.suppliers[0].id)
session[‘quote’] = [] unless session[‘quote’]
@debug = "#{qp.id} - #{qp.quote_id} - " + (qp.new_record? ? ‘new’ :
‘DB’)
session[‘quote’].push(qp)
@debug << " / #{qp.id} - #{qp.quote_id} - " + (qp.new_record? ? ‘new’ :
‘DB’)
end
render :partial => ‘quote_list’
end

Anyone have any insight as to what’s going on here? The auto-saving to
the DB is extremely annoying but I could make it work if I have too.
But the auto-filling the quote_id field is quite unacceptable.

Thanks,
Chris