# Foreign-key confusion

**URL:** <https://www.ruby-forum.com/t/foreign-key-confusion/160963>\
**Category:** Rails\
**Created:** [February 21, 2009, 2:22pm UTC](https://www.ruby-forum.com/t/foreign-key-confusion/160963 "2009-02-21T14:22:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![arichards](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3be4f8/32.png) [@arichards](https://www.ruby-forum.com/u/arichards)\
**Post date:** [February 21, 2009, 2:22pm UTC](https://www.ruby-forum.com/t/foreign-key-confusion/160963/1 "2009-02-21T14:22:20Z")

</div>

Hi,

I am a bit confused about managing the relationship between two of my  
tables:

I have developed a script using scRUBYt that scrapes data from a website  
and dumps the following four fields into my Shows table:

class CreateShows  
create table :shows do |t|  
t.string :date  
t.string :venue  
t.string :info  
t.string :comics

The second table I created is called Theatres:

class CreateTheatres  
create table :theatres do |t|  
t.string :name  
t.string :address  
t.string :website\_url  
t.string :location

The :venue field in the Shows table corresponds to the :name field in  
the Theatres table. How do I set up a relationship whereby Theatres have  
many shows, while defining this (:venue - :name) relationship as the  
link?

I have tried the :set\_primary\_key & :foreign\_key =\> … methods without  
success.  
Does the primary or foreign key always have to be an integer?

Any suggestions would be much appreciated…

---

<div class="post-metadata">

**Author:** ![arichards](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3be4f8/32.png) [@arichards](https://www.ruby-forum.com/u/arichards)\
**Post date:** [February 21, 2009, 3:04pm UTC](https://www.ruby-forum.com/t/foreign-key-confusion/160963/2 "2009-02-21T15:04:26Z")

</div>

On 21 Feb 2009, at 13:22, Ad Richards wrote:

> class CreateShows  
> t.string :name  
> I have tried the :set\_primary\_key & :foreign\_key =\> … methods  
> without  
> success.  
> Does the primary or foreign key always have to be an integer?

A belongs\_to relationship has to go through the primary key of the  
other model and setting the primary key of theatres to be the name  
would be a bad thing (eg what if two towns had a theatre by the same  
name).  
The rails way would be to do away with the venue column and have a  
theatre\_id one instead

---

<div class="post-metadata">

**Author:** ![arichards](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/3be4f8/32.png) [@arichards](https://www.ruby-forum.com/u/arichards)\
**Post date:** [February 21, 2009, 7:31pm UTC](https://www.ruby-forum.com/t/foreign-key-confusion/160963/3 "2009-02-21T19:31:43Z")

</div>

Thanks Fred,

I took your advice and added a theatre\_id column to the Shows table.

Then I added the following callback to the Shows model, in order to  
automatically update the :theatre\_id column with the id of the  
corresponding theatre in the Theatres table.

class Show \< ActiveRecord::Base  
belongs\_to :theatre

after\_create :build\_theatre\_ids

def build\_theatre\_ids  
t = Theatre.find\_by\_name(self.venue)  
self.update\_attributes(:theatre\_id =\> t.id) rescue nil  
end  
end

And it works a treat!
