Ali
Ali

Reputation: 105

How can I set up model inheritance in Rails? I've got a class and two subclasses

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?

Upvotes: 2

Views: 767

Answers (1)

Luke
Luke

Reputation: 4925

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 items table 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 items table in the database.

Once you have the right fields, go ahead and subclass Item:

class Quote < Item
  ...
end

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 show views for each of the subclasses in their own view folder, app/views/links for example, so that you can display them differently on their own pages.

I would also add a app/views/links/_link partial for each of the types, which Rails will intelligently render when you call render @item on the items#index page, for example.

# items/index.html.haml
- @items.each do |item|
  = render item

The above will render app/views/links/_link.html.haml for Link records and app/views/quotes/_quote.html.haml for Quote records.

This should make it pretty easy to achieve the functionality you describe.

Upvotes: 2

Related Questions