Hi All,

After many hours and some inspiration from others on the IRC channel 
finally got it the way I wanted to:

http://blog.lawrencepit.com/2008/10/30/venturing-into-datamapper-many-to-many-self-referential-graphs/




Cheers,
Lawrence

>
> In Rails Recipes there's a proposed solution where you have a users_friends
> table  and a after_create and after_destroy callbacks that create or destroy
> the other bidirectional relationship
>
> Tony
>   
>>
>> I was struggling with the same thing, and in the end came up with
>> something like this:
>>
>>
>> class User
>>   include DataMapper::Resource
>>   has n, user_users
>>
>>   def friends
>>     @friends ||= self.user_users.map {|ss| ss.friend}
>>   end
>>
>>   def friended_by
>>     @friended_by ||= UserUser.all(:friend_id => self.id).map {|ss| ss.user}
>>   end
>> end
>>
>> class UserUser
>>   include DataMapper::Resource
>>
>>   property :user_id, Integer, :key => true
>>   property :friend_id, Integer, :key => true
>>  
>>   belongs_to :user, :class_name => "User", :child_key => [:user_id]
>>   belongs_to :friend, :class_name => "User", :child_key => [:friend_id]
>> end
>>
>>
>> Most probably sub-optimal, but I'm not looking for optimal solutions in
>> the project that I'm working on. ;-)
>>
>> I'm curious though if there's a more succinct way to do this. Because
>> afaics at the moment you can't do this sort of thing nicely in dm yet.
>> (With "nice" I mean it shouldn't be necessary to explicitly define class
>> UserUser and methods +friends+ and +friended_by+ in this example)
>>
>>
>>
>> Lawrence
>>
>>     
>>> Greetings!
>>>
>>> I am pretty new to DataMapper, I can't figure out how to do the
>>> following.
>>>
>>> I have a User model, a user can have friends, which themselves are
>>> users. How can I define this many-to-many relationship on the same
>>> model?
>>>
>>> Thanks!
>>> -- Sinan
>>>       
>>>   
>>>       
>>     
>
>
>
> >
>
>   


--~--~---------~--~----~------------~-------~--~----~
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