andrewdmason-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Apr-24 16:13 UTC
Creating a "friends" association?
Hi, I have a Friend model that looks like this: create_table "friendships", :force => true do |t| t.column "sent_by_user_id", :integer t.column "received_by_user_id", :integer t.column "status", :boolean end I would like to be able to define a :friends association in my user model that returns friendships that are both sent_by and received_by a given user. Any way to do this? I''ve been told that traditionally you just create two friendship records to handle this, but it seems like a cumbersome solution. Thanks, Andrew --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
check out plugin acts as friends at agileweb. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Apr 24, 2007, at 6:13 PM, andrewdmason-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Hi, I have a Friend model that looks like this: > > create_table "friendships", :force => true do |t| > t.column "sent_by_user_id", :integer > t.column "received_by_user_id", :integer > t.column "status", :boolean > end > > > I would like to be able to define a :friends association in my user > model that returns friendships that are both sent_by and received_by a > given user. Any way to do this? > > I''ve been told that traditionally you just create two friendship > records to handle this, but it seems like a cumbersome solution.That''s the canonical solution if it''s OK for the amount of data, otherwise you need to emulate symmetry by hand: sender.friends receiver.friends should contain each other and the nice way to do that is to delegate in AR. That symmetry is then maintained by callbacks on the join model. Additionally, you normally want to apply transitive somehow and the SQL is easier if you have a "direction" to follow, I wrote a little about this here: http://www.advogato.org/article/914.html -- fxn --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
[I sent this a few hours ago but have not receive it back, please excuse if you got it twice.] On Apr 24, 2007, at 6:13 PM, andrewdmason-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Hi, I have a Friend model that looks like this: > > create_table "friendships", :force => true do |t| > t.column "sent_by_user_id", :integer > t.column "received_by_user_id", :integer > t.column "status", :boolean > end > > > I would like to be able to define a :friends association in my user > model that returns friendships that are both sent_by and received_by a > given user. Any way to do this? > > I''ve been told that traditionally you just create two friendship > records to handle this, but it seems like a cumbersome solution.That''s the canonical solution if it''s OK for the amount of data, otherwise you need to emulate symmetry by hand: sender.friends receiver.friends should contain each other and the nice way to do that is to delegate in AR. That symmetry is then maintained by callbacks on the join model. Additionally, you normally want to apply transitive somehow and the SQL is easier if you have a "direction" to follow, I wrote a little about this here: http://www.advogato.org/article/914.html -- fxn --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
andrewdmason-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Apr-25 20:19 UTC
Creating a "friends" association?
Hi, I''m trying to make a friendships table that looks like this: create_table "friendships", :force => true do |t| t.column "sent_by_user_id", :integer t.column "received_by_user_id", :integer t.column "status", :boolean end Where there is 1 record for each friendship, and User.friends returns all friendships through :sent_by user and :received_by user. Creating a User.friends model method is no trouble, but I''d like to make a :friends association to preserve the association magic. Is there a way to write an association that will return both sets sent_by and received_by? Thanks! Andrew --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
andrewdmason-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Apr-25 20:20 UTC
Re: Creating a "friends" association?
sorry, disregard my last message. Problems with google groups... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---