# Question about has\_one

**URL:** <https://www.ruby-forum.com/t/question-about-has-one/70263>\
**Category:** Rails\
**Created:** [September 27, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/question-about-has-one/70263 "2006-09-27T20:35:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nelson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@nelson](https://www.ruby-forum.com/u/nelson)\
**Post date:** [September 27, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/question-about-has-one/70263/1 "2006-09-27T20:35:10Z")

</div>

I have a question about regarding the use of ‘has\_one’ in this scenario:

Schema:

## User

id  
first\_name  
last\_name

## UserMessage

user\_id  
message\_id

## Message

id

Assuming business rules dictate that a Message can have at most ONE User  
(let’s assume a message can be created without a user associated to it),  
these are my questions:

1. How would I use has\_one in my model (if that’s the proper association  
to use) to indicate that a message can have at most one User (without  
having a nullable user\_id column in Message)?

BTW, I chose to have the UserMessage mapping in its own table because if  
I had user\_id in the Message table, I would be forced to use the NULL  
value to indicate that a Message has no User associated with it, and if  
there are tons of Messages with no User, I cringe at the amount of NULL  
values. (But if my logic is wrong, please let me know.)

1. I know in Rails that a ‘has\_one’ association means that if a Message  
has\_one User, then Rails expects a ‘message\_id’ in the User table. Is  
the schema even properly designed for Rails as there is a UserMessage  
table that contains the mappings, and I would tend to want to use  
‘:through’, but that option is not available in a has\_one relationship.  
Of course, if the correct way is indeed to use a nullable user\_id  
column, then this question is moot, and has\_one/belongs\_to is probably  
the way to go.

Thanks in advance!  
Nelson

---

<div class="post-metadata">

**Author:** ![nelson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@nelson](https://www.ruby-forum.com/u/nelson)\
**Post date:** [September 27, 2006, 9:05pm UTC](https://www.ruby-forum.com/t/question-about-has-one/70263/2 "2006-09-27T21:05:51Z")

</div>

> ## UserMessage
> 
> user\_id  
> message\_id
> 
> ## Message
> 
> id  
> I’m not sure that it’s ‘has\_one’ that you need here. ‘has\_one’ implies a  
> one-to-one relationship - like Order -\> Invoice.

Also, I may be wrong, but i don’t think you need that joiner table. The  
joiner table suggests a many-to-many relationship: a message can belong  
to many users, a user can have many messages.

I think what you’re looking for is a one-to-many relationship: A user  
can have many messages, a message belongs to a user.

## User

id  
first\_name  
last\_name

## Message

id  
user\_id

This is wired up in the models like this:

class User \< ActiveRecord::Base  
has\_many :messages  
end

class Message \< ActiveRecord::Base  
belongs\_to :user  
end

Hope that helps,

Steve

---

<div class="post-metadata">

**Author:** ![nelson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@nelson](https://www.ruby-forum.com/u/nelson)\
**Post date:** [September 27, 2006, 9:14pm UTC](https://www.ruby-forum.com/t/question-about-has-one/70263/3 "2006-09-27T21:14:18Z")

</div>

Thanks for your quick response. The way that you wired it up makes  
sense. However, this means that the Message table must have a user\_id  
column, and if I want to indicate that a Message has no User, then  
user\_id must be NULL. And that would mean that if I had a lot of  
Messages with no User, then I’d have store quite a bit of NULL values,  
whereas using a joiner table, I wouldn’t. Generally, I like to avoid  
NULLs - is there a way to design the schema/model so that I can use the  
joiner table? If not, I guess I’ll settle and use a nullable user\_id  
column in Message.

Thanks,  
Nelson

---

<div class="post-metadata">

**Author:** ![nelson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@nelson](https://www.ruby-forum.com/u/nelson)\
**Post date:** [September 27, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/question-about-has-one/70263/4 "2006-09-27T21:57:29Z")

</div>

Nelson H. wrote:

> However, this means that the Message table must have a user\_id  
> column, and if I want to indicate that a Message has no User, then  
> user\_id must be NULL.

Why would you have a message created without a user? Who owns the  
message at that point?

---

<div class="post-metadata">

**Author:** ![nelson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@nelson](https://www.ruby-forum.com/u/nelson)\
**Post date:** [September 27, 2006, 10:08pm UTC](https://www.ruby-forum.com/t/question-about-has-one/70263/5 "2006-09-27T22:08:02Z")

</div>

> Thanks for your quick response. The way that you wired it up makes  
> sense. However, this means that the Message table must have a user\_id  
> column, and if I want to indicate that a Message has no User, then  
> user\_id must be NULL. And that would mean that if I had a lot of  
> Messages with no User, then I’d have store quite a bit of NULL values,  
> whereas using a joiner table, I wouldn’t. Generally, I like to avoid  
> NULLs - is there a way to design the schema/model so that I can use the  
> joiner table? If not, I guess I’ll settle and use a nullable user\_id  
> column in Message.  
> I’m not sure why you would want a message to not have a user, but I  
> guess that depends on the requirements of your application.

Personally, I would favour NULLs over a joiner table in this instance -  
but I’d also question the relationship between User and Message if  
you’re planning on having messages with no associated user.

Steve

---

<div class="post-metadata">

**Author:** ![nelson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@nelson](https://www.ruby-forum.com/u/nelson)\
**Post date:** [September 27, 2006, 11:28pm UTC](https://www.ruby-forum.com/t/question-about-has-one/70263/6 "2006-09-27T23:28:47Z")

</div>

Stephen B. wrote:

> I’m not sure why you would want a message to not have a user, but I  
> guess that depends on the requirements of your application.
> 
> Personally, I would favour NULLs over a joiner table in this instance -  
> but I’d also question the relationship between User and Message if  
> you’re planning on having messages with no associated user.
> 
> Steve

I apologize because Message and User isn’t a good example, but I guess  
I’m trying to model a zero\_to\_many relationship using a joiner table  
rather than a nullable column.
