# Ruby on Rails Relationships

**URL:** <https://www.ruby-forum.com/t/ruby-on-rails-relationships/69840>\
**Category:** Ruby\
**Created:** [September 24, 2006, 11:36pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-relationships/69840 "2006-09-24T23:36:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Murdoc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@Murdoc](https://www.ruby-forum.com/u/Murdoc)\
**Post date:** [September 24, 2006, 11:36pm UTC](https://www.ruby-forum.com/t/ruby-on-rails-relationships/69840/1 "2006-09-24T23:36:42Z")

</div>

Hi all,

I’m not sure if this is an appropriate group to ask for assitance with  
Ruby on Rails, so let me know if it isn’t.

Anyways, I am having trouble figuring out how to setup what would appear  
to be a simple has\_one/belongs\_to relationship.

I have my tables as follows:

```
address = {address_id(PK), street_number, street_name, ....}
venue = {venue_id(PK), venue_name, address_id}

```

Now, logically, each Venue has one address, but an address may be  
associated to multiple entities. For example:

```
contact = {contact_id, contact_given, contact_surname, address_id}

```

If I put the following:

```
class Venue < ActiveRecord::Base
	set_table_name "venue"
	set_primary_key "venue_id"
	has_one :address
end

class Address < ActiveRecord::Base
	set_table_name "address"
	set_primary_key "address_id"
	belongs_to :venue
end

```

Then rails is expecting the venue\_id field to be present in the address  
table. It seems illogical to setup the relationship with the “belongs\_to  
:address” tag in the Venue model, as a Venue does not belong to an  
address, it has an address.

Is there a way to setup this relationship to use the correct linking?

Regards,

---

<div class="post-metadata">

**Author:** ![Murdoc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@Murdoc](https://www.ruby-forum.com/u/Murdoc)\
**Post date:** [September 25, 2006, 12:36am UTC](https://www.ruby-forum.com/t/ruby-on-rails-relationships/69840/2 "2006-09-25T00:36:48Z")

</div>

On 9/24/06, Murdoc [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m not sure if this is an appropriate group to ask for assitance with  
> Ruby on Rails, so let me know if it isn’t.

Please see [http://groups.google.com/group/rubyonrails-talk](http://groups.google.com/group/rubyonrails-talk)

Anyways, I am having trouble figuring out how to setup what would appear  
to

> be a simple has\_one/belongs\_to relationship.

Think of belongs\_to as ‘references’: use it when the model has the  
foreign  
key; use has\_one/many when the model is referenced by a foreign key.

jeremy

---

<div class="post-metadata">

**Author:** ![Murdoc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@Murdoc](https://www.ruby-forum.com/u/Murdoc)\
**Post date:** [September 27, 2006, 8:02am UTC](https://www.ruby-forum.com/t/ruby-on-rails-relationships/69840/3 "2006-09-27T08:02:13Z")

</div>

> Then rails is expecting the venue\_id field to be present in the  
> address table. It seems illogical to setup the relationship with the  
> “belongs\_to :address” tag in the Venue model, as a Venue does not  
> belong to an address, it has an address.

I think you’re mixing the object model with the DB schema. Yes–in  
object modeling, if a venue has an address, you’d expect the Venue class  
to have a pointer to an Address. However, in relational DB schemas,  
it’s the child table (address) that should have a foreign key  
to the parent table (venue)'s primary key.

This becomes more clear if you think about how to represent one-to-many  
relationships. Remember, a column in a table should contain only one  
value.  
You may have many rows in a table, but each column in a row should  
have only one value.

---

<div class="post-metadata">

**Author:** ![Murdoc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@Murdoc](https://www.ruby-forum.com/u/Murdoc)\
**Post date:** [September 28, 2006, 9:37am UTC](https://www.ruby-forum.com/t/ruby-on-rails-relationships/69840/4 "2006-09-28T09:37:17Z")

</div>

Innocent wrote:

> This becomes more clear if you think about how to represent one-to-many  
> relationships. Remember, a column in a table should contain only one value.  
> You may have many rows in a table, but each column in a row should  
> have only one value.

First, it isn’t a one-to-many relationship. Each venue can only ever be  
associated to a single address record. However, that address may be  
shared amongst multiple entities (each of which can only ever be linked  
to one address).

I would have thought that this would be a common scenario that shouldn’t  
be that hard to model in Rails.

---

<div class="post-metadata">

**Author:** ![Murdoc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@Murdoc](https://www.ruby-forum.com/u/Murdoc)\
**Post date:** [September 28, 2006, 9:43am UTC](https://www.ruby-forum.com/t/ruby-on-rails-relationships/69840/5 "2006-09-28T09:43:18Z")

</div>

On 9/28/06, Murdoc [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > to have a pointer to an Address. However, in relational DB schemas,  
> > associated to a single address record. However, that address may be shared  
> > amongst multiple entities (each of which can only ever be linked to one  
> > address).
> 
> I would have thought that this would be a common scenario that shouldn’t  
> be that hard to model in Rails.

Is this correct then?  
class Address \< ActiveRecord::Base  
has\_many :entities  
end

class Entity \< ActiveRecord::Base  
belongs\_to :address  
end

Sounds like a one to many to me…  
The address id then goes as a foreign key into the Entity table  
address\_id INTEGER

Hope I’ve understood you correctly…
