-
Notifications
You must be signed in to change notification settings - Fork 86
Useful Tips
cgthornt edited this page Dec 1, 2014
·
6 revisions
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
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
Now if you try to save:
Product.create! name: 'No Child' # => ActiveRecord::RecordInvalid: Validation failed: Actable can't be blank