Skip to content

Instantly share code, notes, and snippets.

@d11wtq
Created October 11, 2011 04:10
Show Gist options
  • Save d11wtq/1277250 to your computer and use it in GitHub Desktop.
Save d11wtq/1277250 to your computer and use it in GitHub Desktop.
class Message
include DataMapper::Resource
property :id, Serial
belongs_to :user
belongs_to :recipient, 'User', :child_key => [:recipient_id]
end
class User
include DataMapper::Resource
property :id, Serial
has n, :sent_messages, 'Message', :child_key => [:user_id]
has n, :received_messages, 'Message', :child_key => [:recipient_id]
def messages
sent_messages + received_messages
end
end
user1 = User.create
user2 = User.create
Message.create(:user => user1, :recipient => user2)
Message.create(:user => user2, :recipient => user1)
user1.sent_messages #=> correct
user1.received_messages #=> correct
user1.messages #=> wrong... the user_id and the recipient_id get set to the *same* value on all records??
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment