Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails ActiveRecord - inheriting from a base class with no table

Tags:

What i'm looking to do is have a base class for some of my models that has some default activerecord behavior:

class Option < ActiveRecord::Base   has_many :products    def some_method     #stuff   end    #etc..etc.. end  class ColorOption < Option   #stuff... end   class FabricOption < Option   #stuff... end 

However, I want ColorOption and FabricOption to each be in their own tables. I do NOT want to use STI or have a table for the base class "Option". The only way I've gotten this to work is with some non-inheritance metaprogramming magic. But I wondered if there was a way to tell AR that the base class does not need a table. Its just there for extra behavior, and to put the other subclasses in their own table as usual.

Thanks, Craig

like image 372
fregas Avatar asked Aug 20 '10 16:08

fregas


1 Answers

What you want is an abstract class:

class Option < ActiveRecord::Base   self.abstract_class = true end  class ColorOption < Option   ... end  class FabricOption < Option   ... end 
like image 115
François Beausoleil Avatar answered Sep 25 '22 10:09

François Beausoleil