I have a Book and Download model that share many attributes, so my goal is to inherit the common attributes from a DownloadableResource model.
Had a look at STI, but I went the abstract base model class way instead:
models:
class DownloadableResource < ActiveRecord::Base self.abstract_class = true attr_accessible :title, :url, :description, :active, :position validates :title, :url, :description, presence: true scope :active, where(active: true).order(:position) end class Book < DownloadableResource attr_accessible :cover_url, :authors validates :cover_url, :authors, presence: true end class Download < DownloadableResource attr_accessible :icon_url validates :icon_url, presence: true end
migrations:
class CreateDownloadableResources < ActiveRecord::Migration def change create_table :downloadable_resources do |t| t.string :title t.string :url t.text :description t.boolean :active, default: false t.integer :position t.timestamps end end end class CreateBooks < ActiveRecord::Migration def change create_table :books do |t| t.string :cover_url t.string :authors t.timestamps end end end class CreateDownloads < ActiveRecord::Migration def change create_table :downloads do |t| t.string :icon_url t.timestamps end end end
After migration, when I create a new Book the result is far from expected:
> Book.new
=> #<Book id: nil, cover_url: nil, authors: nil, created_at: nil, updated_at: nil>
Can somebody please shed some light on how to implement the Abstract Base Model Class technique so ActiveRecord models can share common code via inheritance yet be persisted to different database tables?
[:books, :downloads].each do |table| change_table table do |t| t.text :description # ... end end
– Shawana