rupert wrote:
That's what I initially had.  It work fine for ActiveRecord (from which
I'm migrating), but not for DataMapper.
    

by jove you're right - how odd!

I've could get the functionality by doing:

case e.type.to_s
when "EventOne"
  puts 'One'
when "EventTwo"
  puts 'Two'
else
  puts "Unknown event type #{e.type}"
end

it makes me feel unclean tho!
  
Here's a full script that demonstrates the problem.  An object retrieved via an association is somehow different than one retrieved directly.

---------------------------------

#!/usr/bin/ruby

require 'rubygems'
require 'dm-core'

DataMapper::Logger.new(STDOUT, :debug)
adapter = DataMapper.setup(:default, "sqlite3::memory:")

class PlayItem
  include DataMapper::Resource

  property :id,       Integer, :key => true
  property :event_id, Integer
  property :name,     Text

  belongs_to :event
end

class Event
  include DataMapper::Resource

  property :id,   Integer, :key => true
  property :type, Discriminator
  property :name, Text

  has n, :play_items
end

class EventOne < Event
end

class EventTwo < Event
end

def find_class(event)
  case event
  when EventOne
    puts 'One'
  when EventTwo
    puts 'Two'
  else
    puts "Unknown event type #{event}"
  end
end

DataMapper.auto_migrate!

e = EventOne.create(:id => 1, :name => 'E1')
p = e.play_items.build(:id => 1, :name => 'Test')
e.save

e2 = Event.first
p2 = PlayItem.first
puts e, e2, p.event, p, p2, e.play_items[0]

find_class e          # This works.
find_class e2         # So does this.
find_class p.event    # This doesn't.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "DataMapper" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to