One model or many?
I'm going to build a basic forum-type of application -- something with different discussions on art, music, and literature. A user would go to one of three pages based on those categories and read or participate in a discussion.
Should I create 3 different models (ArtDiscussion, MusicDiscussion, and LitDiscussion) and let them live independently? Or should I create 1 model and have my 3 sections derive from that?
I can see benefits to both but I'm wondering if there is a best-practice. The benefit to creating 3 different models is that I can easily keep them all distinct. Each model lends itself to a different part of my site. Someone who is interested in discussing 'art' will only interact with the art discussion. If I ever want to add another discussion category, I'll simply create a new model for that category.
On the other hand, it seems cleaner to have just one model -- Discussion -- with a column called "category" that will contain either "art", "music", or "literature." Since all other columns will be the same, there doesn't seem to be a strong reason to have multiple models. However, every time someone goes to the art discussion page, my application will have to sort through every discussion topic and pull out only those with a category equal to "art." Is this a problem? Any suggestions?
I would use two model classes: Discussion and Topic.
The topics table would have a name and an id field plus perhaps management field such as created_at, updated_at, and created_by.
The discussions table would include a topic_id field plus the others you were intending to have.
In the topic model I would add:
and in the discussion model I would add:
Then each Topic object would have a discussions method returning an array of all discussions on that topic.
So for example you could do this:
art.discussions would then contain all the discussion items on the topic of Art.
art = Topic.find(:first, :conditions => ["name = ?", 'Art'])
The best thing about this approach is that all you have to do to create a discussion on other topics is add a new topic to the topic table.
Hey Reggie, I like the suggestion.
I'd probably also want to create a model called Comment (or Thread). A Discussion would have_many comments and a Comment would belong_to a Discussion. For example, right now I'm adding a comment to the 'One model or many' discussion. Does that sound correct to you?
Sounds good to me.
It is sometimes easiest to think at the object level than the table level. A comment is a different object from a discussion. As such the class Comment will have a separate model and therefore a separate table.
What makes one object different from another is it properties and behaviour. ArtDiscussion, MusicDiscussion, or LitDiscussion would all have the same properties and behaviour. The only difference is the categorisation by discussion topic. If you have a way of defining the topic type within a discussion object you can use the same discussion object for all three discussion types. The Topic class gives you the way to define the discussion types.
A comment on the other hand will have different properties to a discussion. It therefore also needs its own class defining model and table.
I would suggest checking out http://beast.caboo.se/ for ideas/help on a rails forum implementation. The code is extremely concise and high-quality, it should give you a lot of ideas on how to solve your problem elegantly.
That sounds like just what I've been looking for -- thanks, I'll check it out!
I have another related question about using a RESTful approach, but I'll put it in a new thread since it's kind of a separate topic.