[LRUG] Base class for model

David A. Black dblack at rubypal.com
Fri Sep 11 08:23:06 PDT 2009


Hi --

On Fri, 11 Sep 2009, Simon Sebright wrote:

> Hi
>  
> 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.
>  
> Is this somehow supported, or should I be using a module/mixin instead?

Here's how you can do it:

   class Parent < ActiveRecord::Base
     self.abstract_class = true
   end

and then inherit from Parent. This tells AR that Parent isn't pegged
to a table.


David

-- 
David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com
Ruby/Rails training, mentoring, consulting, code-review
Latest book: The Well-Grounded Rubyist (http://www.manning.com/black2)


More information about the Chat mailing list