Skip to main content
0 online
iwz iwz

Here's a design problem I'm running into.

I have a bunch of different objects that all need to accept comments.

Topics (News, Journals, ForumThreads) Single Table Inheritance
Pictures
Events
Faqs
Polls

It's obvious that News, Journals and ForumThreads are all pretty much the same thing, and fit nicely into the Topics table. The other objects really don't fit in. They all have SOME common attributes, though, like a title and body (except for Polls).

Yet, all of these objects need to be commentable. I'm having trouble figuring out a clean way to model this.

The comments table references the various types with two identifiers: thread_type, and generic_id. thread_type is basically an enum(News,Journal,ForumThread,Picture,Event,Faq,Poll). And generic_id is the id of the referenced table.

I can't decide whether I should combine the remaining commentable tables into the Topics table and manage everything at the Topic level, or stick with the current schema and try to make it work... i dunno

D
dgiaimoOG 2003

Why do you need to reference both thread_type and generic_id? Couldn't you make sure that generic_id is unique across the various tables? If not, then you could add another table (maybe called "COMMENTABLES"?) that maps the local generic_id to a globally unique id which you could then reference from the comments table. If you have properties that are common to things which can be commented on, then you could add them to that table as well.

iwz iwz

Yeah.. that's why; the legacy schema has a separate table and sequence for every type of topic. Kind of unwieldy.

I've been thinking about it a bit. I may go down the road of merging all the commentable types into a single table. It'll just be a bulky table. Events, which has LOTS of unique fields, I may make a child table for that holds all the unique stuff. Not sure if that's the best way to do it tho.

yay yayOG 2004

The bottom line here is you really want to deal with a commenting functionality the same way for every entity, right?

Personally, when it gets down to objects having similar fields sometimes it doesn't make sense to try to pool them into a common category because it could be a maintenance nightmare trying to add anything in the future.

iwz iwz

Yeah, I want to have common functionality.

Last night I worked on the problem a bit more using the existing schema (it's a big job to change the schema and update all the foreign keys in the comments table).

I came up with a RESTful URL scheme for posting comments:

/comments/new/:type/:id

For example, if you POST a comment to /comments/new/journal/256, it will take "journal" as the type, and use it like so:
@obj = params [:type].camelize.constantize.new.find(params [:id])

This retrieves the correct object to add the comment to, so then I just call
@obj.comments.build(params [:comment])

This is a partial solution to the problem. Gives me a central place to add comments to topics.

I'm still not happy with it overall, though. I mean, it works, but it's really not the cleanest solution.

deanh77 deanh77Founder

thats a pretty neat way of doing it. very DRY, but maybe a bit hack-ish. but if it keeps the code tight, it could be a good thing.

Welcome Back to eZabel

It's been a while. Here's what's new.

eZabel Lore

A complete history of our community — stats, Hall of Fame, legendary threads, and more.

View the Lore →

Everything Preserved

All 225,969 pieces of content from 2000–2014 are here — forums, messages, journals, photos, polls, and events.

💎

Gems

Spot something you love — a legendary comment, a classic thread, a great photo? Log in and click the diamond icon to mark it as a Gem. Add a note about why it's special. The best stuff surfaces on the Gems page.