# Relationship woes

**URL:** <https://www.ruby-forum.com/t/relationship-woes/54760>\
**Category:** Rails\
**Created:** [March 14, 2006, 12:59am UTC](https://www.ruby-forum.com/t/relationship-woes/54760 "2006-03-14T00:59:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 14, 2006, 12:59am UTC](https://www.ruby-forum.com/t/relationship-woes/54760/1 "2006-03-14T00:59:25Z")

</div>

I have two tables:  
class PgmUpdate \< ActiveRecord::Base  
has\_many :pgm\_visits, :foreign\_key =\> ‘pgm\_update\_fk’  
…  
and  
class PgmVisit \< ActiveRecord::Base  
belongs\_to :pgm\_update, :foreign\_key =\>  
‘pgm\_update\_fk’, :dependent =\> true  
…

According to the has\_many docs ([http://api.rubyonrails.org/classes/](http://api.rubyonrails.org/classes/)  
ActiveRecord/Associations/ClassMethods.html#M000471)  
collection\<\<(object, â?¦) - adds one or more objects to the collection  
by setting their foreign keys to the collectionâ??s primary key.

But when I add a new, unsaved PgmVisit to an unsaved PgmUpdate the  
PgmVisit still has a nil foreign key and no pgm\_update to show for.  
Are the docs failing to mention something about unsaved records? I  
had similar unpredictable problems with WebObjects which is why I am  
now learning Rails.

A related questing is when is the best time to set a custom primary  
key? Is it required for collection\<\< to work? I would prefer to not  
set it if possible since the DB will generate one, but I will if I  
have to.

-John

–  
John S.  
Computing Staff - Webmaster  
Kavli Institute for Theoretical Physics  
University of California, Santa Barbara  
removed\_email\_address@domain.invalid  
(805) 893-6307

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 14, 2006, 1:46am UTC](https://www.ruby-forum.com/t/relationship-woes/54760/2 "2006-03-14T01:46:21Z")

</div>

The PgmUpdate object needs to be saved before the foreign keys will get  
assigned correctly. If possible, save the PgmUpdate before adding any  
visits. If you are worried about partial lists/info wrap it in a  
transaction:  
PgmUpdate.transaction do  
…  
end

You shouldn’t need to set any keys manually. Rails/ActiveRecord will  
take  
care of it.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 14, 2006, 5:13pm UTC](https://www.ruby-forum.com/t/relationship-woes/54760/3 "2006-03-14T17:13:14Z")

</div>

Ok, that sounds reasonable, but is it safe for a transactions to span  
multiple request-responses? I want a form for users to add visits,  
but it must be able to rollback the transaction if the form is never  
submitted or cancelled, do transactions timeout? Should I just work  
around the problem by associating and saving everything at the last  
submission?

-John

–  
John S.  
Computing Staff - Webmaster  
Kavli Institute for Theoretical Physics  
University of California, Santa Barbara  
removed\_email\_address@domain.invalid  
(805) 893-6307
