Understanding this model relationship (possibly a HABTM and a belongs_to ?)

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

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?

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,

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?

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.

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.

Ok, I’ll look into that. thanks.