I'm planning an application that would store different types of content. Some attributes would be used by all content types (title, description etc.), other attributes would be specific to the content type (eg. a photo would have a thumbnail_url).
I'd like to avoid single table inheritance, because additional content types are likely to be added overtime so im uncomfortable adding additional attributes to the content table which wont be used by all entities.
Therefore, I'd like a base table that stores the attributes shared by all content types, and then additional tables for the extended data.
For example the base 'content' table would contain the following rows:
Content Table
Code:
title
description
created_on
extended_type
Then the 'article', 'photo' and 'audio' tables would contain the extended data for each appropriate type.
Article Table
Photo Table
Code:
full_url
thumbnail_url
Audio Table
Code:
length
artist
album
From what I've read this appears to be possible using Rails' polymorphic associations but I'm a bit confused on how to implement this. Would this be achieved using something familiar to the following?
Code:
class link < ActiveRecord::Base
belongs_to :content
belongs_to :extendedinfo, :polymorphic => true
end
class Content < ActiveRecord::Base
has_one :link
has_one :extendedinfo, :through => :link
end
class Article < ActiveRecord::Base
has_one :link, :as => :extentioninfo
has_one :content, :through => :link
end
class Photo < ActiveRecord::Base
has_one :link, :as => :extentioninfo
has_one :content, :through => :link
end
class Audio < ActiveRecord::Base
has_one :link, :as => :extentioninfo
has_one :content, :through => :link
end
Am I on the right track here, or is there another way of achieving similar? Thanks.
Bookmarks