SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Zealot HenriIV's Avatar
    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?

  2. #2
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could just make Property a subclass of ActiveRecord::Base, and then extend the various properties from that?

  3. #3
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought you could do multiple inheritance. This works:

    Code:
    class Foo < ActiveRecord::Base
    end
    
    class Bar < Foo
    end
    Is that even multiple inheritance?

  4. #4
    SitePoint Zealot HenriIV's Avatar
    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

  5. #5
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Zealot HenriIV's Avatar
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •