[LRUG] Base class for model

Chris Mear chrismear at gmail.com
Fri Sep 11 08:30:30 PDT 2009


On 11 Sep 2009, at 16:17, Simon Sebright wrote:

> I was attempting to do a bit of refactoring.  I had one model class,  
> in which I had defined a few operations. Then along came a second  
> one, where I wanted the same operations, so I started to create a  
> common base class for them, which naturally inherits from  
> ActiveRecord::Base
>
> Now, I get all sorts of errors, and it looks like rails is taking  
> the base class name and assuming it'll find a table relating to it,  
> which in my case it won't.

Yeah, ActiveRecord assumes when you have models inheriting from each  
other, and you have a column called 'type' (do you?), that you're  
trying to do single-table inheritance. This would explain the  
behaviour you're seeing.

> Is this somehow supported, or should I be using a module/mixin  
> instead?

I've heard talk before that by overriding the default name for the  
type column that is used by STI, you can disable STI. In your model  
class, put something like

   self.inheritance_column = 'no_sti_please'

or some other column name that doesn't actually exist in your table.

I have no idea if this actually works.

The less hacky approach and more self-evident approach would be to use  
a module.

Chris




More information about the Chat mailing list