Question about has_one

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

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

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

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?

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs