John McGrath
2006-Jan-14 06:43 UTC
[Rails] nuby: do models have to inherit directly from ActiveRecord?
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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060114/a267578f/attachment.html
Carl-Johan Kihlbom
2006-Jan-14 07:56 UTC
[Rails] nuby: do models have to inherit directly from ActiveRecord?
You should take a look at Single Table Inheritance (STI): http://wiki.rubyonrails.com/rails/pages/SingleTableInheritance / CJ On 1/14/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. > > 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''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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Chris Wong
2006-Jan-14 08:10 UTC
[Rails] nuby: do models have to inherit directly from ActiveRecord?
Have you tried using Ruby module as mixin? Chris 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. > > 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''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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Eric Hodel
2006-Jan-14 10:34 UTC
[Rails] nuby: do models have to inherit directly from ActiveRecord?
On Jan 14, 2006, at 12:11 AM, rails-request@lists.rubyonrails.org 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> > 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.That''s how you do it.> 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.A few things are harder than they should be, like subclassing ActiveRecord::Base. Rather than make the common thing easy, ActiveRecord reserves subclassing for a seldom-used feature.> Anyways, apologies for being a dunce. Any guidance on how to best > handle the > abstract base class question would be much appreciated.Bite the bullet and add those methods. -- Eric Hodel - drbrain@segment7.net - http://segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com
Eric Hodel
2006-Jan-14 10:41 UTC
[Rails] nuby: do models have to inherit directly from ActiveRecord?
On Jan 14, 2006, at 12:11 AM, rails-request@lists.rubyonrails.org wrote:> On 1/13/06, John McGrath <jmcgrath@whoi.edu> 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. > > Have you tried using Ruby module as mixin?Ruby is an OO language so subclassing should Just Work. The single table inheritance hack prevents developers from performing code reuse the natural OO way. There''s more use cases for code reuse via inheritance than for single table inheritance. -- Eric Hodel - drbrain@segment7.net - http://segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com
Mark Reginald James
2006-Jan-14 11:42 UTC
[Rails] Re: nuby: do models have to inherit directly from ActiveRecord?
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.
Joshua Sierles
2006-Jan-14 15:16 UTC
[Rails] nuby: do models have to inherit directly from ActiveRecord?
Since ActiveRecord creates a direct mapping to database tables, there would be a case here for mixins. STI is not a seldom used feature, and Ruby provides the mixin facility just for these types of situations where you need to inherit functionality from two places. Joshua Sierles On 1/14/06, Eric Hodel <drbrain@segment7.net> wrote:> > On Jan 14, 2006, at 12:11 AM, rails-request@lists.rubyonrails.org wrote: > > > On 1/13/06, John McGrath <jmcgrath@whoi.edu> 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. > > > > Have you tried using Ruby module as mixin? > > Ruby is an OO language so subclassing should Just Work. The single > table inheritance hack prevents developers from performing code reuse > the natural OO way. There''s more use cases for code reuse via > inheritance than for single table inheritance. > > -- > Eric Hodel - drbrain@segment7.net - http://segment7.net > This implementation is HODEL-HASH-9600 compliant > > http://trackmap.robotcoop.com > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060114/ff6e72c2/attachment.html
Joe Van Dyk
2006-Jan-14 16:35 UTC
[Rails] nuby: do models have to inherit directly from ActiveRecord?
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/Tech/Ruby/Connections.rdoc You can''t do: class Media < AR; end class Book < Media; end class CD < Media; end; ?
Jules Jacobs
2006-Jan-14 17:30 UTC
[Rails] Re: nuby: do models have to inherit directly from ActiveReco
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/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 -- Posted via http://www.ruby-forum.com/.
Ben Munat
2006-Jan-14 19:22 UTC
[Rails] Re: nuby: do models have to inherit directly from ActiveReco
Jules Jacobs wrote:> Joe Van Dyk wrote: >> >>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 >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
Joe Van Dyk
2006-Jan-14 21:06 UTC
[Rails] Re: nuby: do models have to inherit directly from ActiveReco
On 1/14/06, Ben Munat <bent@munat.com> wrote:> Jules Jacobs wrote: > > Joe Van Dyk wrote: > >> > >>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 > > > > 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).
Eric Hodel
2006-Jan-14 21:44 UTC
[Rails] nuby: do models have to inherit directly from ActiveRecord?
On Jan 14, 2006, at 7:17 AM, rails-request@lists.rubyonrails.org wrote:> On 1/14/06, Eric Hodel <drbrain@segment7.net> wrote: >> >> On Jan 14, 2006, at 12:11 AM, rails-request@lists.rubyonrails.org >> wrote: >> >>> On 1/13/06, John McGrath <jmcgrath@whoi.edu> 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. >>> >>> Have you tried using Ruby module as mixin? >> >> Ruby is an OO language so subclassing should Just Work. The single >> table inheritance hack prevents developers from performing code reuse >> the natural OO way. There''s more use cases for code reuse via >> inheritance than for single table inheritance. > > Since ActiveRecord creates a direct mapping to database tables, > there would > be a case here for mixins. STI is not a seldom used feature, and Ruby > provides the mixin facility just for these types of situations > where you > need to inherit functionality from two places.Proper factoring of code should not be made difficult in the interests of a feature than not everybody uses. It would make more sense to tell people "call this method to set this class up for STI" then to tell people "sorry, you can''t use inheritance with ActiveRecord::Base the way you do everywhere else in Ruby and in every other OO language unless you add this hack". You should certainly not need to use Module#include to hack around broken inheritance. Mixins are for functionality that does not fit in the class hierarchy. -- Eric Hodel - drbrain@segment7.net - http://segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com