Forum: Ruby on Rails Creating a "friends" association?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
(Guest)
on 2007-04-24 21:56
(Received via mailing list)
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
mixplate (Guest)
on 2007-04-24 22:01
check out plugin acts as friends at agileweb.
Xavier N. (Guest)
on 2007-04-25 04:18
(Received via mailing list)
[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, removed_email_address@domain.invalid wrote:

> 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
Xavier N. (Guest)
on 2007-04-25 06:08
(Received via mailing list)
On Apr 24, 2007, at 6:13 PM, removed_email_address@domain.invalid wrote:

> 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
This topic is locked and can not be replied to.