I am new to DataMapper and must be missing something simple. I have am
trying to reproduce an ActiveRecord setup which has
class User < ActiveRecord::Base
has_many :avatars, :as => :subject, :dependent => :destroy
end
class Family < ActiveRecord::Base
has_many :avatars, :as => :subject, :dependent => :destroy
end
class Avatar < ActiveRecord::Base
belongs_to :subject, :polymorphic => true
end
The Avatar class has database columns subject_id and subject_type
I did some searching and saw dm-polymorphic which I could not get to
work. I also tried to roll my own where I have
class User
include DataMapper::Resource
has n, :avatars, :child_key => [:subject_id]
end
class Avatar
include DataMapper::Resource
property :id, Serial
property :subject_id, Integer, :nullable => false
property :subject_type, String, :nullable => false
def subject
return nil if subject_type.blank? || subject_id.nil?
klass = Kernel.const_get(subject_type)
(klass.ancestors.include? DataMapper::Resource) ? klass.get
(subject_id) : nil
end
def subject=(entity)
self.subject_id = entity.id
self.subject_type = entity.class.to_s
end
end
The problem I am having is when I do something like
@user.avatars << @avatar
@user.save
Understandably, the subject_type field is not being set for an
instance of Avatar. Is there a way I can make this work? I saw
http://pastie.org/214893
but did not follow the additional property being declared for the has
and belongs_to relationships and how to add this to my classes to get
the needed behavior. Any help is appreciated.
Mark Lynn
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---