Options for modeling content objects

1 view
Skip to first unread message

Amjad

unread,
Mar 15, 2012, 3:40:45 PM3/15/12
to microcommunity
I'll try to summarize the results of my recent search about this issue.

The problem is that we will have different types of content objects (pages, questions, bookmarks..). Although they share basic attributes and behaviors, they have a lot of different attributes and behaviors. Here using ActiveRecord would make some how hard to accomplish this.

  • The first way is to use Single Table inheritance. It is only good where you have a couple of models with limited different attributes.
  • The second way is to use has_one association. Although it makes the common attributes in a single model (let it be content_properties), the code is going to be ugly, e.g. you'll have to type @page.content_properties.title to access this content. In other words you created has_a relation while you want is_a relationship.
  • The third option is to use modules. So imagine you include "act_as_content" module in each content object. The problem is that this allows you to share behavior but not database attributes, except if you find a tricky way to assure that content properties attributes are in the database schema of each model (e.g. using generators). This is quite ugly as well.
  • Another choice is use Abstract Active Record. For the first glance it seems good, but we still have the problem of shared attributes that are duplicated in the child classes.
  • The last choice is to simulate multi-table inheritance. The code to do this seems a bit ugly but it would give us the result we want. Here's the best link that describes how to do so: http://mediumexposure.com/multiple-table-inheritance-active-record/#mti-the-short-story
So the real solution to the problem is to use multi table choice. I'm not sure whether existing implementations are safe and stable to use. There is a project that aims to make that easier, which also summarizes existing solutions:
http://peterhamilton.github.com/citier/existing_solutions.html

I hope this can give you an idea about existing options. I'll try to study the costs and benefits of each ones and update you soon inshalla.

Amjad




Amjad

unread,
Mar 15, 2012, 3:43:12 PM3/15/12
to microcommunity
I think I need to make sure about the modules choice.

I'm currently having a look at act_as_taggable gem:
https://github.com/mbleigh/acts-as-taggable-on

Amjad

unread,
Mar 15, 2012, 3:47:50 PM3/15/12
to microcommunity
I found a simple gem to allow table inheritance:
http://www.agilewebdevelopment.com/plugins/inherits_from

although it does seem to be popular.

Amjad

unread,
Mar 15, 2012, 3:50:17 PM3/15/12
to microcommunity
Here are interesting options involving class table inheritance, but the problem is that they are not quite popular:
http://stackoverflow.com/questions/3499215/class-table-inheritance-in-rails-3

Amjad

unread,
Mar 16, 2012, 2:38:17 AM3/16/12
to microcommunity
I'm sorry for bothering you with all this detail... it seems at last that the association solution is the commonly used although it is ugly
http://groups.diigo.com/group/wiki_techie/content/real-world-rails-part-3-optimizing-activerecord-4575627

Anyway I think I will not work on modelling the content for now, I'll try to implement the following feature on a separate branch and see what it needs. Then we might be able to make the right decision.

Feature: manage wiki pages
In order share objective ideas without the need of a wiki website
As a user
I want to manage my own wiki pages

Scenario: posting a wiki page
Given there is a logged in user
When he posts a wiki page
Then the wiki page should appear on his wall

Cool! This is my first real-world experiment using Cucumber, BDD and Outside-In Developement.
Reply all
Reply to author
Forward
0 new messages