0

I have a rails model Book, with STI-inherited models Fiction and NonFiction

While book holds a lot of common logic, I'd like to forbid creation of the parent Book model. Just wondering about the most elegant method for doing that in Rails - any suggestions appreciated

PlankTon
  • 12,443
  • 16
  • 84
  • 153

2 Answers2

3

You can set it abstract:

class Book < ActiveRecord::Base
  self.abstract_class = true
  ...
end
BvuRVKyUVlViVIc7
  • 11,641
  • 9
  • 59
  • 111
2

You could raise error in Book's initializer

class Book
  def initialize *args
    raise "Can't create a Book" if self.class == Book
    super # if it's not the Book, proceed with ActiveRecord initialization
  end
end
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367