# How to create 0:M or 0:1 relationships in ActiveRecord?

**URL:** <https://www.ruby-forum.com/t/how-to-create-0-m-or-0-1-relationships-in-activerecord/74007>\
**Category:** Rails\
**Created:** [October 29, 2006, 2:52am UTC](https://www.ruby-forum.com/t/how-to-create-0-m-or-0-1-relationships-in-activerecord/74007 "2006-10-29T02:52:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [October 29, 2006, 2:52am UTC](https://www.ruby-forum.com/t/how-to-create-0-m-or-0-1-relationships-in-activerecord/74007/1 "2006-10-29T02:52:20Z")

</div>

I’m comfortable using has\_many and belongs\_to for 1:M relationships.  
But how do you implement 0:1 and 0:M relationships? Thanks!

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [October 29, 2006, 7:32am UTC](https://www.ruby-forum.com/t/how-to-create-0-m-or-0-1-relationships-in-activerecord/74007/2 "2006-10-29T07:32:14Z")

</div>

Taylor S. wrote:

> I’m comfortable using has\_many and belongs\_to for 1:M relationships.  
> But how do you implement 0:1 and 0:M relationships? Thanks!

What does that mean?

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [October 29, 2006, 7:38am UTC](https://www.ruby-forum.com/t/how-to-create-0-m-or-0-1-relationships-in-activerecord/74007/3 "2006-10-29T07:38:21Z")

</div>

Maybe a better question is, “Can Active Record use :has\_many and  
:belongs to without FK definitions being present in the database?”

For example, I have buildings who have landlords. However, since this  
data is input by renters, landlord data will often not be submitted. So  
i need to indicate that builds belong to landlords but AR complains when  
there is no landlord to attribute it to. If I remove the foreign key  
constraint how do I define the relationship?

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [October 29, 2006, 8:41am UTC](https://www.ruby-forum.com/t/how-to-create-0-m-or-0-1-relationships-in-activerecord/74007/4 "2006-10-29T08:41:20Z")

</div>

Taylor S. wrote:

> Maybe a better question is, “Can Active Record use :has\_many and  
> :belongs to without FK definitions being present in the database?”
> 
> For example, I have buildings who have landlords. However, since this  
> data is input by renters, landlord data will often not be submitted. So  
> i need to indicate that builds belong to landlords but AR complains when  
> there is no landlord to attribute it to. If I remove the foreign key  
> constraint how do I define the relationship?

You’re certainly allowed to have a :belongs\_to relationship to nil.  
i.e., create a building such that building.landlord\_id=nil.

Just don’t “validates\_presence\_of” on landlord\_id.

Jake
