Nuby: do models have to inherit directly from ActiveRecord?


#1

Hello,

I have a few models – book, cd, dvd – for which I’d like to have an
abstract base superclass to hold some common stuff. That abstract class,
I
was thinking, would inherit from ActiveRecord.

Didn’t work, though, and looking around, I found this:
http://wiki.rubyonrails.com/rails/pages/HowtoMakeAbstractModel
http://wiki.rubyonrails.com/rails/pages/HowtoMakeAbstractModel. Which
looks
pretty hackish to me. Is there any best way or recommendations on how to
achieve this kind of behavior? I know I could modify ActiveRecord
directly,
but that seems brittle – to run my app on later versions of rails, I’d
have
to remember to re-modify ActiveRecord.

What’s really brittle, though, is my brain. This is my first RoR app,
and
parts of rails (convention over config, built-in ORM and testing) I
immediately loved. But some stuff seems like it would be, well, [puts on
fireproof suit] easier in Java. I’m still sort of waiting for it all to
come
together in my mind.

Anyways, apologies for being a dunce. Any guidance on how to best handle
the
abstract base class question would be much appreciated.

John
http://fryolator.com


#2

You should take a look at Single Table Inheritance (STI):
http://wiki.rubyonrails.com/rails/pages/SingleTableInheritance

/ CJ


#3

Have you tried using Ruby module as mixin?

Chris


#4

On 1/13/06, John McGrath removed_email_address@domain.invalid wrote:

Hello,

I have a few models – book, cd, dvd – for which I’d like to have an
abstract base superclass to hold some common stuff. That abstract class, I
was thinking, would inherit from ActiveRecord.

I think this applies:
http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/Connections.rdoc

You can’t do:
class Media < AR; end
class Book < Media; end
class CD < Media; end;

?


#5

Joe Van D. wrote:

On 1/13/06, John McGrath removed_email_address@domain.invalid wrote:

Hello,

I have a few models – book, cd, dvd – for which I’d like to have an
abstract base superclass to hold some common stuff. That abstract class, I
was thinking, would inherit from ActiveRecord.

I think this applies:
http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/Connections.rdoc

You can’t do:
class Media < AR; end
class Book < Media; end
class CD < Media; end;

?

But you CAN do:

module Media
#…
end

class Book < AR
include Media
end

class CD < AR
include Media
end


#6

John McGrath wrote:

I have a few models – book, cd, dvd – for which I’d like to have an
abstract base superclass to hold some common stuff. That abstract
class, I was thinking, would inherit from ActiveRecord.

Didn’t work, though, and looking around, I found this:
http://wiki.rubyonrails.com/rails/pages/HowtoMakeAbstractModel. Which
looks pretty hackish to me. Is there any best way or recommendations on
how to achieve this kind of behavior? I know I could modify ActiveRecord
directly, but that seems brittle – to run my app on later versions of
rails, I’d have to remember to re-modify ActiveRecord.

What I did once was to put the common stuff in a module, called say
Media
in your case, and put ‘include Media’ at the top of each model class.

Instance methods will include directly, but for class methods that you
want to call without using the “Media.” prefix you have to define a
self.included(model) method in the module that defines the class methods
in a model.class_eval block.


We develop, watch us RoR, in numbers too big to ignore.


#7

On 1/14/06, Ben M. removed_email_address@domain.invalid wrote:

class CD < AR
include Media
end

Just to clarify here: Joe was NOT saying that you CAN’T have a base class… note the “?”
after the “You can’t do:” bit. He was saying “why can’t you just do…?”, right Joe?

Yes, that’s right.

It seems that either approach will work, though now I’m even more confused. I thought the
STI requirement in rails precluded base model classes (that don’t map to tables). From the
Dave T. blog entry it looks like it’s okay as long as you don’t try to call db methods
on the base class.

right?

Yeah, I think it’s fine as long as the base class is abstract (i.e.
you don’t try to instantiate it or try to call any db methods with
it).


#8

Jules J. wrote:

But you CAN do:
include Media
end

Just to clarify here: Joe was NOT saying that you CAN’T have a base
class… note the “?”
after the “You can’t do:” bit. He was saying “why can’t you just
do…?”, right Joe?

It seems that either approach will work, though now I’m even more
confused. I thought the
STI requirement in rails precluded base model classes (that don’t map to
tables). From the
Dave T. blog entry it looks like it’s okay as long as you don’t try
to call db methods
on the base class.

right?

b