User has many topics or subtopics

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

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:

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

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

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

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

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...