0
votes

I'm trying to design a schema for an email solution so that I could access the incoming and sent messages on the User object using DataMapper. The associations "inbox" and "sent" don't do what's intended. What am I doing wrong? Thanks in advance!

I've the following so far (after reading a bit and copying the friends example from DM website) --

class User
    include DataMapper::Resource
    property :id, Serial
    property :name, String, :required=>true
    property :email, String, :required=>true, :unique=>true
    property :password, String, :required=>true

    has n, :messages, :child_key=>[:source_id, :target_id]
    has n, :inbox, 'Message', :through=>:messages, :via=>:target
    has n, :sent, 'Message', :through=>:messages, :via=>:source
end

class Message
    include DataMapper::Resource
    property :id, Serial
    property :subject, String, :required=>true
    property :body, String

    belongs_to :source, 'User', :key=>true
    belongs_to :target, 'User', :key=>true
end
1

1 Answers

1
votes

I'm answering my own question -- hope it helps someone

The following change fixes the problem I've been having --

class User
    ...

    has n, :inbox, 'Message', :child_key=>[:target_id]
    has n, :sent, 'Message', :child_key=>[:source_id]
end

Everything else, remains the same...