Forum: Ruby on Rails nuby: do models have to inherit directly from ActiveRecord?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ac31d0d6453b50ad6fdfe16742273424?d=identicon&s=25 John McGrath (Guest)
on 2006-01-14 07:45
(Received via mailing list)
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/HowtoMakeA...
http://wiki.rubyonrails.com/rails/pages/HowtoMakeA.... 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
E24b2a1d71b7365186a934a09ee6f7c3?d=identicon&s=25 Carl-Johan Kihlbom (Guest)
on 2006-01-14 08:57
(Received via mailing list)
You should take a look at Single Table Inheritance (STI):
http://wiki.rubyonrails.com/rails/pages/SingleTabl...

/ CJ
0047fe8d018cbe3fb63c065bc3b591e1?d=identicon&s=25 Chris Wong (Guest)
on 2006-01-14 09:12
(Received via mailing list)
Have you tried using Ruby module as mixin?

Chris
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-01-14 12:44
(Received via mailing list)
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/HowtoMakeA.... 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.
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-14 17:38
(Received via mailing list)
On 1/13/06, John McGrath <jmcgrath@whoi.edu> 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/Tec...

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

?
71f1b6b2c3fd9af2e8c52618fb91caa6?d=identicon&s=25 Jules Jacobs (jules)
on 2006-01-14 18:30
Joe Van Dyk wrote:
> On 1/13/06, John McGrath <jmcgrath@whoi.edu> 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/Tec...
>
> 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
4005a47a8f2ceee49670b920593c1d52?d=identicon&s=25 Ben Munat (Guest)
on 2006-01-14 20:25
(Received via mailing list)
Jules Jacobs 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 Thomas 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
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-01-21 15:40
(Received via mailing list)
On 1/14/06, Ben Munat <bent@munat.com> 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 Thomas 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).
This topic is locked and can not be replied to.