Created
October 11, 2011 04:10
-
-
Save d11wtq/1277250 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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