Suppose I’m creating a blog with specific types of posts.
I have a model called an Item. An Item has a name (string) and a link (string) to a source or something. An Item has_and_belongs_to_many :tags (tags are just strings stored in their own database).
I want to extend Items into two different models: Quote and Link.
A Quote is an Item with some content (text) attached to it.
A Link is pretty much the same as an Item.
Both Texts and Links need to have names and links from the Item class, but a Quote has an extra field (the content:text). I’m only giving Link a subclass because it’s not supposed to be thought of as a superclass of Quote.
I want to be able to display Links and Quotes on their own pages, and render them differently. I also want to be able to list all Items on the front page, and render them differently (Quotes need to show their content).
Can anyone help with this?
This is a pretty simple case of ActiveRecord inheritance, and STI (Single Table Inheritance) should suite your needs just fine.
All you really need to do is make sure that your
itemstable has all the necessary fields to cover each of its descendants’ attributes, plus a type (string) field, which is used internally by ActiveRecord to keep track of the class of each record.In this case, it looks like you need to add a content (text) field and the type (string) field to the
itemstable in the database.Once you have the right fields, go ahead and subclass
Item:etc, etc.
I recommend you use a single ItemsController for both types and keep as much functionality as possible in the parent model, as this generally keeps things cleaner and simpler.
I would add
showviews for each of the subclasses in their own view folder,app/views/linksfor example, so that you can display them differently on their own pages.I would also add a
app/views/links/_linkpartial for each of the types, which Rails will intelligently render when you callrender @itemon theitems#indexpage, for example.The above will render
app/views/links/_link.html.hamlforLinkrecords andapp/views/quotes/_quote.html.hamlforQuoterecords.This should make it pretty easy to achieve the functionality you describe.