Forum: Ruby on Rails Adding ActiveRecord objects to the session

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
33eb32997ccb3a5497cb1df8b075c2b2?d=identicon&s=25 Chris (Guest)
on 2006-01-31 00:28
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
This topic is locked and can not be replied to.