# Asymmetrical HABTM(?)

**URL:** <https://www.ruby-forum.com/t/asymmetrical-habtm/143608>\
**Category:** Rails\
**Created:** [July 21, 2008, 6:26am UTC](https://www.ruby-forum.com/t/asymmetrical-habtm/143608 "2008-07-21T06:26:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stan.baptista](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@stan.baptista](https://www.ruby-forum.com/u/stan.baptista)\
**Post date:** [July 21, 2008, 6:26am UTC](https://www.ruby-forum.com/t/asymmetrical-habtm/143608/1 "2008-07-21T06:26:10Z")

</div>

What is the best way to set up a relationship between two tables where  
table-1 has a one-to-one relationship with table-2 but table-2 has a  
one-to-many relationship with table-1?

For example, suppose there is an events application (seminars, forums,  
conferences, etc). Each event has only one address but since many  
events can occur at the same location throughout the year, a given  
address can belong to many events.

Thanks,  
Stan

---

<div class="post-metadata">

**Author:** ![stan.baptista](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@stan.baptista](https://www.ruby-forum.com/u/stan.baptista)\
**Post date:** [July 21, 2008, 6:43am UTC](https://www.ruby-forum.com/t/asymmetrical-habtm/143608/2 "2008-07-21T06:43:06Z")

</div>

On Jul 21, 2008, at 12:25 AM, stan.baptista wrote:

> Stan  
> That’s just a normal association:

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

class Address \< ActiveRecord::Base  
has\_many :events  
end

Don’t let the seemingly backward “belongs\_to :address” fool you. The  
‘belongs\_to’ association is on the model that contains the foreign key  
(address\_id in this example) even though you say “Each event has only  
one address…” – the “has … one” is misleading in this case.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![stan.baptista](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@stan.baptista](https://www.ruby-forum.com/u/stan.baptista)\
**Post date:** [July 21, 2008, 6:57am UTC](https://www.ruby-forum.com/t/asymmetrical-habtm/143608/3 "2008-07-21T06:57:07Z")

</div>

> Don’t let the seemingly backward “belongs\_to :address” fool you. The  
> ‘belongs\_to’ association is on the model that contains the foreign key  
> (address\_id in this example) even though you say “Each event has only  
> one address…” – the “has … one” is misleading in this case.

Got it. Thanks Rob.

---

<div class="post-metadata">

**Author:** ![stan.baptista](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@stan.baptista](https://www.ruby-forum.com/u/stan.baptista)\
**Post date:** [July 21, 2008, 5:16pm UTC](https://www.ruby-forum.com/t/asymmetrical-habtm/143608/4 "2008-07-21T17:16:37Z")

</div>

That sounds like a plain 1:many to me. Address has\_many :events, and  
Event belongs\_to :address.
