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
On 29/10/2008 15:48, "Lawrence Pit" <[EMAIL PROTECTED]> wrote:
>
>
>
> 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
-~----------~----~----~----~------~----~------~--~---