# Understanding this model relationship (possibly a HABTM and a belongs\_to ?)

**URL:** <https://www.ruby-forum.com/t/understanding-this-model-relationship-possibly-a-habtm-and-a-belongs-to/193096>\
**Category:** Rails\
**Created:** [August 11, 2010, 6:22am UTC](https://www.ruby-forum.com/t/understanding-this-model-relationship-possibly-a-habtm-and-a-belongs-to/193096 "2010-08-11T06:22:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [August 11, 2010, 6:22am UTC](https://www.ruby-forum.com/t/understanding-this-model-relationship-possibly-a-habtm-and-a-belongs-to/193096/1 "2010-08-11T06:22:03Z")

</div>

I’m pretty newb and trying to get a handle on setting up a particular  
relationship with my models.

For sake of this discussion (real app isn’t with tools but for  
illustration,  
using tools):

To start with (more complicated in a bit);

- A User can have multiple tools
- A single tool can only belong to one ToolGroup
- A User can belong to multiple ToolGroups

To model this relationship, I was thinking:

User  
has\_many :tools  
has\_and\_belong\_to\_many :tool\_groups

Tool  
has\_one :tool\_group  
belongs\_to :user

ToolGroup  
has\_and\_belongs\_to\_many :users

So first question, is the above set up correctly?

Once the above is set up correctly, I want to have it set up where a  
ToolGroup can also be ‘owned’ by a User. In other words, a user will set  
up  
a ToolGroup and they are the owner, but other users can belong to this  
ToolGroup. How do I model that? Do I need to set up another model that  
is  
called “Owner” that is really a “User”? This doesn’t seem right. In  
reality  
the db table “ToolGroup” would just need an “ownerID” that would be a fk  
to  
the User table, but I’m not sure how to model that since ToolGroup  
already  
has a HABTM to users. Can I have a HABTM to Users and also a belongs\_to  
:User?

Thanks for any help

---

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [August 11, 2010, 7:54pm UTC](https://www.ruby-forum.com/t/understanding-this-model-relationship-possibly-a-habtm-and-a-belongs-to/193096/2 "2010-08-11T19:54:02Z")

</div>

My initial example is probably confusing. Here is another shot at it in  
something that has more meaning:

A User can create multiple â€œnoticesâ€ ( eg â€œCorporate meeting noticeâ€,  
â€œBig  
Sale Itemsâ€, â€œJob Wantedâ€ )  
A User can create multiple â€œnotice groupsâ€ (eg â€œCorporate Noticesâ€,  
â€œPublic  
Noticesâ€, â€œMy Miscellaneous Noticesâ€)  
Every notice should belong to an â€œnotice groupâ€  
A User needs to be able to subscribe to multiple â€œnotice groupsâ€ so that  
they could see the notices in the different groups theyâ€™re subscribed  
to.

To model this relationship, I was thinking:

User  
has\_many :notices  
has\_and\_belong\_to\_many :notice\_groups

Notice  
has\_one :notice\_group  
belongs\_to :user

NoticeGroup  
has\_and\_belongs\_to\_many :users

So first question, is the above set up correctly?

How would I also model that a â€œNoticeGroupâ€ is â€˜owned/createdâ€™ by a  
particular user? Right now the HABTM on NoticeGroup seems to imply that  
the  
NoticeGroup can belong to many Users but I also need it to show that its  
owned/created by a particular User (owner\_id concept on NoticeGroup?),  
can I  
have a HABTM :users and a belongs\_to :User in the same NoticeGroup model  
definition?

On Wed, Aug 11, 2010 at 12:20 AM, Rick R [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> - A User can belong to multiple ToolGroups
> 
> called “Owner” that is really a “User”? This doesn’t seem right. In reality  
> the db table “ToolGroup” would just need an “ownerID” that would be a fk to  
> the User table, but I’m not sure how to model that since ToolGroup already  
> has a HABTM to users. Can I have a HABTM to Users and also a belongs\_to  
> :User?
> 
> Thanks for any help

–  
Rick R

---

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [August 11, 2010, 8:26pm UTC](https://www.ruby-forum.com/t/understanding-this-model-relationship-possibly-a-habtm-and-a-belongs-to/193096/3 "2010-08-11T20:26:33Z")

</div>

Rick R wrote:

> My initial example is probably confusing. Here is another shot at it in  
> something that has more meaning:
> 
> A User can create multiple â€œnoticesâ€ ( eg â€œCorporate meeting noticeâ€,  
> â€œBig  
> Sale Itemsâ€, â€œJob Wantedâ€ )  
> A User can create multiple â€œnotice groupsâ€ (eg â€œCorporate Noticesâ€,  
> â€œPublic  
> Noticesâ€, â€œMy Miscellaneous Noticesâ€)

Are these notice groups specific to the one user, or are they more like  
global discussion topics?

Can notice groups contain notices created by users other than the one  
who created the group?

> Every notice should belong to an â€œnotice groupâ€  
> A User needs to be able to subscribe to multiple â€œnotice groupsâ€ so that  
> they could see the notices in the different groups theyâ€™re subscribed  
> to.
> 
> To model this relationship, I was thinking:
> 
> User  
> has\_many :notices  
> has\_and\_belong\_to\_many :notice\_groups
> 
> Notice  
> has\_one :notice\_group

That should be belongs\_to, not has\_one.

> belongs\_to :user
> 
> NoticeGroup  
> has\_and\_belongs\_to\_many :users
> 
> So first question, is the above set up correctly?

Maybe, maybe not. Depends on the questions above.

> How would I also model that a â€œNoticeGroupâ€ is â€˜owned/createdâ€™ by a  
> particular user? Right now the HABTM on NoticeGroup seems to imply that  
> the  
> NoticeGroup can belong to many Users but I also need it to show that its  
> owned/created by a particular User (owner\_id concept on NoticeGroup?),  
> can I  
> have a HABTM :users and a belongs\_to :User in the same NoticeGroup model  
> definition?

Sure. Just give the associations different names.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Rick\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@Rick\_R](https://www.ruby-forum.com/u/Rick_R)\
**Post date:** [August 11, 2010, 9:23pm UTC](https://www.ruby-forum.com/t/understanding-this-model-relationship-possibly-a-habtm-and-a-belongs-to/193096/4 "2010-08-11T21:23:37Z")

</div>

On Wed, Aug 11, 2010 at 2:26 PM, Marnen Laibow-Koser  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Are these notice groups specific to the one user, or are they more like  
> global discussion topics?

They are “created by one user” but multiple users can subscribe to them:

“A User needs to be able to subscribe to multiple â€œnotice groupsâ€ so  
that  
they could see the notices in the different groups theyâ€™re subscribed  
to.”

> Can notice groups contain notices created by users other than the one  
> who created the group?

No.

> Sure. Just give the associations different names.

Ok, I’ll look into that. thanks.
