# Having nil / non-nil relationships

**URL:** <https://www.ruby-forum.com/t/having-nil-non-nil-relationships/48864>\
**Category:** Rails\
**Created:** [November 25, 2005, 10:06pm UTC](https://www.ruby-forum.com/t/having-nil-non-nil-relationships/48864 "2005-11-25T22:06:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonlee9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@jasonlee9](https://www.ruby-forum.com/u/jasonlee9)\
**Post date:** [November 25, 2005, 10:06pm UTC](https://www.ruby-forum.com/t/having-nil-non-nil-relationships/48864/1 "2005-11-25T22:06:15Z")

</div>

Hi all,

I have a problem I’m trying to figure out. I have a table/object that  
can have either a nil or non-nil id that points to another table. For  
instance:

Customers  
±-------------+  
| id | \<- not null  
| name | \<- not null  
| company\_id | \<- nullable  
| company\_name | \<- nullable  
±-------------+

Companies  
±--------------+  
| cid |  
| name |  
±--------------+

So I want to be able to allow a customer to either select a company  
name from a drop down list, or enter a name we don’t have in the  
database, or not have to enter a name at all. In my Customer class  
I’m trying a

has\_one :company, :foreign\_key =\> “cid” # I know there is no FK  
constraint

but the sql comes out saying something like

SELECT \* FROM companies WHERE ( companies.cid = 3251 ) LIMIT 1

where 3251 is the id of the Customer obj/table. So AR looks like it’s  
trying to fetch me the association, but it’s using the Customers  
table id as the PK id for the Companies table, which I don’t want. If  
I _must_ have a FK from Customers.company\_id -\> Companies.cid I  
suppose I can pass in a -1 default when the user does not select or  
use a company (will AR do this??), but I was kinda hoping I wouldn’t  
have to. It would just seem nice to check for a nil  
customer.company\_id and then display the customer.company or not.

Any suggestions? Thanks,

- jason

---

<div class="post-metadata">

**Author:** ![jasonlee9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@jasonlee9](https://www.ruby-forum.com/u/jasonlee9)\
**Post date:** [November 26, 2005, 3:20am UTC](https://www.ruby-forum.com/t/having-nil-non-nil-relationships/48864/2 "2005-11-26T03:20:44Z")

</div>

Why not just use ‘id’ instead of ‘cid’ as the column name in the  
companies  
table?

---

<div class="post-metadata">

**Author:** ![jasonlee9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@jasonlee9](https://www.ruby-forum.com/u/jasonlee9)\
**Post date:** [November 26, 2005, 5:45am UTC](https://www.ruby-forum.com/t/having-nil-non-nil-relationships/48864/3 "2005-11-26T05:45:38Z")

</div>

For starters, you want a “belongs\_to” relationship rather than a  
“has\_one” relationship.

Pete.

---

<div class="post-metadata">

**Author:** ![jasonlee9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@jasonlee9](https://www.ruby-forum.com/u/jasonlee9)\
**Post date:** [November 26, 2005, 6:35pm UTC](https://www.ruby-forum.com/t/having-nil-non-nil-relationships/48864/4 "2005-11-26T18:35:06Z")

</div>

You’re right, that works. However, I guess, after reading many times,  
I still don’t quite grasp the difference (in my case) between using  
has\_one and belongs\_to. I would think that customer would have one  
company and not ‘belong’ to a company; I would think, if anything,  
that employee would belong to a company.

I suppose I need to really connect in my mind what AR is actually  
doing with a belongs\_to. Anyway, for now, this works correctly and I  
guess until it really sinks in I’ll try has\_one and if that doesn’t  
work, belongs\_to.

Thanks,

- jason

---

<div class="post-metadata">

**Author:** ![jasonlee9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4491bb/32.png) [@jasonlee9](https://www.ruby-forum.com/u/jasonlee9)\
**Post date:** [November 26, 2005, 7:39pm UTC](https://www.ruby-forum.com/t/having-nil-non-nil-relationships/48864/5 "2005-11-26T19:39:46Z")

</div>

the simple way to remember is this:

if table A contains a reference (the \*\_id column) to table B, then model  
A  
belongs\_to Model B and Model B has\_many or has\_one Model A.
