# User has many topics or subtopics

**URL:** <https://www.ruby-forum.com/t/user-has-many-topics-or-subtopics/171155>\
**Category:** Rails\
**Created:** [June 27, 2009, 5:27am UTC](https://www.ruby-forum.com/t/user-has-many-topics-or-subtopics/171155 "2009-06-27T05:27:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ADADAD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ADADAD](https://www.ruby-forum.com/u/ADADAD)\
**Post date:** [June 27, 2009, 5:27am UTC](https://www.ruby-forum.com/t/user-has-many-topics-or-subtopics/171155/1 "2009-06-27T05:27:44Z")

</div>

Hi,

So i have users in the system and i have topics. Topics also  
have\_many subtopics. What is the cleanest way to setup the AR  
relationships if a user can either have\_many topics or subtopics ?  
Its basically a dual select box dropdown, where the second dropdown is  
optional.

Is the best to have user\_topics, and user\_subtopics tables and manage  
that way ? or combine into one table with STI and a type ?

Thanks  
AD

---

<div class="post-metadata">

**Author:** ![ADADAD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ADADAD](https://www.ruby-forum.com/u/ADADAD)\
**Post date:** [June 27, 2009, 10:42am UTC](https://www.ruby-forum.com/t/user-has-many-topics-or-subtopics/171155/2 "2009-06-27T10:42:45Z")

</div>

AD wrote:

> Hi,
> 
> So i have users in the system and i have topics. Topics also  
> have\_many subtopics. What is the cleanest way to setup the AR  
> relationships if a user can either have\_many topics or subtopics ?  
> Its basically a dual select box dropdown, where the second dropdown is  
> optional.
> 
> Is the best to have user\_topics, and user\_subtopics tables and manage  
> that way ? or combine into one table with STI and a type ?
> 
> Thanks  
> AD

Maybe better like this:

topic.rb  
belongs\_to :user  
has\_many :subtopics

user.rb  
has\_many :topics  
has\_many :subtopics, :through =\> :topics

subtopic.rb  
belongs\_to :user  
belongs\_to :topic

Try smth like this, and read this one:

> **[Active Record Associations — Ruby on Rails Guides](https://guides.rubyonrails.org/association_basics.html#the-has-many-through-association)**
>
> Active Record AssociationsThis guide covers the association features of Active Record.After reading this guide, you will know: How to declare associations between Active Record models. How to understand the various types of Active Record...

---

<div class="post-metadata">

**Author:** ![ADADAD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ADADAD](https://www.ruby-forum.com/u/ADADAD)\
**Post date:** [June 27, 2009, 10:52am UTC](https://www.ruby-forum.com/t/user-has-many-topics-or-subtopics/171155/3 "2009-06-27T10:52:19Z")

</div>

2009/6/27 Andrey O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > Â Is the best to have user\_topics, and user\_subtopics tables and manage
> 
> user.rb  
> has\_many :topics  
> has\_many :subtopics, :through =\> :topics
> 
> subtopic.rb  
> belongs\_to :user  
> belongs\_to :topic

I imagine it may need HABTM relationships for users\_topics and  
users\_subtopics if topics and subtopics can belong to multiple users.

Also I think the above will not allow a user to have a subtopic  
without a topic, which I think AD asked for, though he/she may not  
have meant that. Though if this was meant then I would suggest maybe  
an error in the design. AD can you clarify the requirement?

Colin

---

<div class="post-metadata">

**Author:** ![ADADAD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ADADAD](https://www.ruby-forum.com/u/ADADAD)\
**Post date:** [June 27, 2009, 9:09pm UTC](https://www.ruby-forum.com/t/user-has-many-topics-or-subtopics/171155/4 "2009-06-27T21:09:44Z")

</div>

yea sorry if you have a subtopic, you have a topic since a subtopic  
belongs to a topic. So if I have TopicA, TopicB, and TopicA has  
SubTopicD and SubTopicE, a user can have

TopicB, SubtopicE. Since SubTopicE belongs to TopicA they would also  
have that. The requirement was really that they didnt have to choose  
a subtopic if they only wanted to choose a Topic. But choosing a  
subtopic intrinsically gets you a topic by association of that  
subtopic to its parent topic.

AD

---

<div class="post-metadata">

**Author:** ![ADADAD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ADADAD](https://www.ruby-forum.com/u/ADADAD)\
**Post date:** [June 29, 2009, 3:23am UTC](https://www.ruby-forum.com/t/user-has-many-topics-or-subtopics/171155/5 "2009-06-29T03:23:45Z")

</div>

yes sorry, a topic can belong to many users. For subtopics we had  
went the subtopics\_users approach, but now that a new requirement came  
in to support a user having either a topic with no subtopic as well as  
a subtopic directly (impying topic).

Thanks

---

<div class="post-metadata">

**Author:** ![ADADAD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ADADAD](https://www.ruby-forum.com/u/ADADAD)\
**Post date:** [June 27, 2009, 9:30pm UTC](https://www.ruby-forum.com/t/user-has-many-topics-or-subtopics/171155/6 "2009-06-27T21:30:49Z")

</div>

2009/6/27 AD [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> AD

You have not clarified whether a topic can belong to more one user.  
If so then you need HABTM associations. Do you need further help? I  
presume you have worked through the rails guide on associations.

Colin

---

<div class="post-metadata">

**Author:** ![ADADAD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@ADADAD](https://www.ruby-forum.com/u/ADADAD)\
**Post date:** [June 29, 2009, 4:30am UTC](https://www.ruby-forum.com/t/user-has-many-topics-or-subtopics/171155/7 "2009-06-29T04:30:02Z")

</div>

AD wrote:

> yes sorry, a topic can belong to many users. For subtopics we had  
> went the subtopics\_users approach, but now that a new requirement came  
> in to support a user having either a topic with no subtopic as well as  
> a subtopic directly (impying topic).

If I were to jump in late, and only read that graph, without reading the  
rest of  
the thread (purely as a rhetorical technique, mind you)…

Then u appear to have the Composite Design Pattern. Give each topic a  
super\_topic\_id. If a topic has a pointer to another topic, it’s a  
subtopic.  
Forcing them all to belong to the same user is then a business rules  
issue, not  
a database issue. I suspect that system is most flexible…

–  
Phlip  
[http://twitter.com/Pen\_Bird](http://twitter.com/Pen_Bird)
