SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
Thread: [RoR] Multiple Inheritance
-
Mar 3, 2006, 09:16 #1
- Join Date
- Jun 2004
- Location
- France
- Posts
- 129
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
[RoR] Multiple Inheritance
Hi,
Let's say that I have a Property class and subclasses like Home, Plot, Appartment...
but as there is no multiple inheritance in Ruby, how do I implement it in Rails? (a model is already a subclass of ActiveRecord::Base)
Mixins didn't seem to be appropriate, is Home < Property enough?
-
Mar 3, 2006, 10:53 #2
You could just make Property a subclass of ActiveRecord::Base, and then extend the various properties from that?
-
Mar 3, 2006, 11:35 #3
I thought you could do multiple inheritance. This works:
Code:class Foo < ActiveRecord::Base end class Bar < Foo end
-
Mar 3, 2006, 13:03 #4
- Join Date
- Jun 2004
- Location
- France
- Posts
- 129
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It works but only displays Properties instead of Homes
-
Mar 3, 2006, 17:09 #5
Are you trying to use single-table inheritance? This should work automatically. You have a Property model which maps to a properties table, which has a column called "type". Values of type such as "home", "plot" etc. would then determine which subclass of Property is used.
The downside to this is that you have to store all of the columns that might be unique to your subclasses in the single properties table, with null values where they do not apply. The alternative is class table inheritance but Rails does not support this (yet).
http://wiki.rubyonrails.com/rails/pa...bleInheritance
Google for RubyOnRails class table inheritance - there seems to be some stuff out there but nothing concrete at the moment.
-
Mar 4, 2006, 06:17 #6
- Join Date
- Jun 2004
- Location
- France
- Posts
- 129
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for the tip, I'm gonna check this. I searched for abstract class + ruby without success
Bookmarks