Forum: Ruby on Rails subclassing vs mixins, which one should be used?

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.
D299e69ca3532ffad7b12377e5e42dd5?d=identicon&s=25 BushyMark (Guest)
on 2008-12-10 00:58
(Received via mailing list)
Hey all,

I have two models in my rails project that share a lot of traits. They
each have the same 4 properties, and now I need to add 2-3 methods to
each one that will be the same. In the spirit of DRY I am looking for
a solution to write these methods only once.

Both of these models already inherit from ActiveRecord . . . and I
didn't know if it was safe to just "whip up" a super class for them
both and have them inherit the methods from that(competent at rails
and ruby, but grasping for that intermediate level here). I know that
Enumerable is a mixin that is included in arrays and hashes, etc and
that I can create modules. (where should I put them in rails?). Does
anyone have any good information on subclassing in rails vs mixins?
Tutorials, good write ups or books to help me understand further? Even
your own advice would be great.

Thanks ahead of time
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-10 01:02
(Received via mailing list)
On Dec 9, 11:58 pm, BushyMark <bushym...@gmail.com> wrote:
> Hey all,
>
> I have two models in my rails project that share a lot of traits. They
> each have the same 4 properties, and now I need to add 2-3 methods to
> each one that will be the same. In the spirit of DRY I am looking for
> a solution to write these methods only once.
>
> Both of these models already inherit from ActiveRecord . . . and I
> didn't know if it was safe to just "whip up" a super class for them

You can do that, as long as the base class has
class MyAbstractClass < ActiveRecord::Base
  self.abstract_class = true
  ...
end
(or else rails will assume you're trying to do single table
inheritance)

> both and have them inherit the methods from that(competent at rails
> and ruby, but grasping for that intermediate level here). I know that
> Enumerable is a mixin that is included in arrays and hashes, etc and
> that I can create modules. (where should I put them in rails?). Does
> anyone have any good information on subclassing in rails vs mixins?

Doesn't really matter. Rails will find them in the lib or models
folders among others.
Exactly when you go down either is a judgement call really.

Fred
D299e69ca3532ffad7b12377e5e42dd5?d=identicon&s=25 BushyMark (Guest)
on 2008-12-10 01:14
(Received via mailing list)
Fred, thanks for the info!

I like the idea of creating a super class, if only for the naming
convention I have setup. I just wanted to check my syntax though.

The two models are called BlockSchedule and DaySchedule.

Lets say I wanted them both to inherit the properties of a class
called Schedule (see why I like the sub classing idea? easy to
remember naming conventions, :-) ). By your instructions I create the
class

class Schedule < ActiveRecord::Base
self.abstract_class = true
...[shared methods go here]
end

and DaySchedule I would re-write to be
DaySchedule < Schedule
...
end

And now DaySchedule's class methods (everything from what I have
written to save, etc . . . stay the same?
Sorry if the question seems remedial, I really appreciate the help!



On Dec 9, 7:01 pm, Frederick Cheung <frederick.che...@gmail.com>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-10 01:20
(Received via mailing list)
On 10 Dec 2008, at 00:13, BushyMark wrote:

> remember naming conventions, :-) ). By your instructions I create the
> end
>
> And now DaySchedule's class methods (everything from what I have
> written to save, etc . . . stay the same?

That's the theory!

Fred
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2008-12-10 04:03
(Received via mailing list)
I don't know anything about your app, but if you don't use STI (ie.
you leave abstract_class = true) you'll never be able to tell the
difference b/n the two classes.

That is... you could..

create a BlockSchedule with ID=1
create a DaySchedule with ID=2

BlockSchedule.all => returns both records.

If you use STI it would only return the first.  To get them all you'd
do Schedule.all

Anyway, maybe this isn't what you want and if so ignore me :)
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-10 10:01
(Received via mailing list)
On Dec 10, 3:02 am, Philip Hallstrom <phi...@pjkh.com> wrote:
> I don't know anything about your app, but if you don't use STI (ie.  
> you leave abstract_class = true) you'll never be able to tell the  
> difference b/n the two classes.
>
> That is... you could..
>
> create a BlockSchedule with ID=1
> create a DaySchedule with ID=2
>
> BlockSchedule.all => returns both records.

That's not true. With the parent class being an abstract class they'd
be in two completely separate tables.

Fred
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2008-12-10 19:10
(Received via mailing list)
>> BlockSchedule.all => returns both records.
>
> That's not true. With the parent class being an abstract class they'd
> be in two completely separate tables.

Ah. Sweet!  Didn't know that.  Thought from your other message that
toggle just told rails to stop looking around for STI (and the type
field). Good to know!
10f7e00c1c5c60e2173995eb16f4ffc7?d=identicon&s=25 pepe (Guest)
on 2008-12-11 02:25
(Received via mailing list)
Head First Design Patterns is an excellent book IMHO for OO
application design. It's geared towards Java but it has excellent
points. One of them being 'favor composition over inheritance'. The
use of modules in Rails gives so much flexibility that I think going
that route would be the best/simplest solution.

Pepe
This topic is locked and can not be replied to.