#550 ✓not-applicable
Ward Asbridge

Models and class inheritance oddness

Reported by Ward Asbridge | September 4th, 2008 @ 04:10 PM

While experimenting with base model classes, I've found that DM doesn't seem to handle child models which extend from base classes. This scenario is the closest I got it to working - calling "rake dm:db:automigrate" causes an SQL error, as DM tries to create an empty media_bases table. If I add a dummy field to the base class, such as "property :id, Serial", then DM does create a dummy media_bases table, but doesn't create a media_videos table at all.

lib/media.rb


class Media
  class Base

    include DataMapper::Resource

    #property :id, Serial

    def inherited(model)
      model.class_eval do
        property :id,             Serial
        property :title,          String,   :length => 255
        property :description,    Text
        property :created_at,     DateTime
        property :updated_at,     DateTime
        property :deleted_at,     DateTime
        property :created_user,   String, :length => 255
        property :updated_user,   String, :length => 255
        property :deleted_user,   String, :length => 255
        property :filesize,       Integer
      end
    end

  end
end

app/models/media_video.rb


require 'media'
class Media
  class Video < Media::Base

    include DataMapper::Resource

    property :runtime,        Integer
    property :aspect,         String

  end
end

Comments and changes to this ticket

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) January 8th, 2009 @ 02:20 PM

    • State changed from “new” to “unconfirmed”
    • Assigned user cleared.
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) February 21st, 2009 @ 01:38 AM

    • Tag set to dm-core
    • State changed from “unconfirmed” to “not-applicable”

    The usage of a true inherited method would likely cause a bit of problems because DM::Resource uses its own to handle inheritance. However, in this case, you've defined it as an instance method so it probably would have had no effect.

    The key to getting this to work is to add a discriminator field, like this:

    
      property :type, Discriminator
    

    DM only really works with inheritance when using a discriminator property. While this may change at some point in the future, it is how it is currently designed to work.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket

Tags

Pages