Skip to content
cgthornt edited this page Dec 1, 2014 · 6 revisions

Add Indexes to Migration

Adding an index causes lookups to perform much faster as the table size grows:

create_table :products do |t|
  t.actable index: true
  t.string :name
end

Make the Base Class "Abstract"

You might want to prevent your base class from not having a child class:

product = Product.create! name: 'No Child'
product.specific # => nil

You should add both a database-level constraint and a validation.

The migration:

create_table :products do |t|
  t.actable null: false
  t.string :name
end

And in your model:

class Product < ActiveRecord::Base
  actable
  validates_presence_of :actable
end
Clone this wiki locally