Discussion:
Simple link or self-referential associations?
Dave Amos
2006-11-19 01:29:40 UTC
Permalink
Hi there!

I'm working on a little recipe-sharing application, and I want people to be
able to click on a link in a user's profile to add them as "friends". I
imagined it working where once the link was clicked, the user_id of the
friendee and the current_user.id of the friender would be stored in a
database table called friends. I asked about this on a ruby forum, and most
people told me that the best way to do this was using self-referential
has_many :through associations (see:
http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through).
They also concluded that it was well out of my league, and I guess I agree.
Is there a way to do it my way, without the associations (except for maybe a
simple users has_many friends sort of thing)?

Thanks, and let me know if you need more information!

Dave
David Turnbull
2006-11-19 02:07:31 UTC
Permalink
You probably don't need the complexity of a :through. But you are
definitely looking at a self-referential. Here's the example from
Rails Recipes:

class Person < ActiveRecord::Base
has_and_belongs_to_many :friends,
:class_name => "Person",
:join_table => "friends_people",
:association_foreign_key => "friend_id",
:foreign_key => "person_id"
end

Don't forget to add a friends_people join table to the database.
Then you do controller stuff like: person1.friends << person2

Ignore that (confusing) blog post you were referred to and go buy
Rails Recipes if you need the rest of the details to make this work.
Recipe 18 will cut and paste to exactly what you want to do.

Maybe get a new forums too.

-david
Post by Dave Amos
Hi there!
I'm working on a little recipe-sharing application, and I want
people to be able to click on a link in a user's profile to add
them as "friends". I imagined it working where once the link was
clicked, the user_id of the friendee and the current_user.id of the
friender would be stored in a database table called friends. I
asked about this on a ruby forum, and most people told me that the
best way to do this was using self-referential has_many :through
associations (see: http://blog.hasmanythrough.com/articles/
2006/04/21/self-referential-through). They also concluded that it
was well out of my league, and I guess I agree. Is there a way to
do it my way, without the associations (except for maybe a simple
users has_many friends sort of thing)?
Thanks, and let me know if you need more information!
Dave
_______________________________________________
PDXRuby mailing list
IRC: #pdx.rb on irc.freenode.net
http://lists.pdxruby.org/mailman/listinfo/pdxruby
Dave Amos
2006-11-19 02:10:09 UTC
Permalink
Thanks, I just bought Rails Recipes and I guess I must've missed that one.
I'll go try it. Thanks!
Post by David Turnbull
You probably don't need the complexity of a :through. But you are
definitely looking at a self-referential. Here's the example from Rails
class Person < ActiveRecord::Base
has_and_belongs_to_many :friends,
:class_name => "Person",
:join_table => "friends_people",
:association_foreign_key => "friend_id",
:foreign_key => "person_id"
end
Don't forget to add a friends_people join table to the database. Then you
do controller stuff like: person1.friends << person2
Ignore that (confusing) blog post you were referred to and go buy Rails
Recipes if you need the rest of the details to make this work. Recipe 18
will cut and paste to exactly what you want to do.
Maybe get a new forums too.
-david
Hi there!
I'm working on a little recipe-sharing application, and I want people to
be able to click on a link in a user's profile to add them as "friends". I
imagined it working where once the link was clicked, the user_id of the
friendee and the current_user.id of the friender would be stored in a
database table called friends. I asked about this on a ruby forum, and most
people told me that the best way to do this was using self-referential
http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through).
They also concluded that it was well out of my league, and I guess I agree.
Is there a way to do it my way, without the associations (except for maybe a
simple users has_many friends sort of thing)?
Thanks, and let me know if you need more information!
Dave
_______________________________________________
PDXRuby mailing list
IRC: #pdx.rb on irc.freenode.net
http://lists.pdxruby.org/mailman/listinfo/pdxruby
_______________________________________________
PDXRuby mailing list
IRC: #pdx.rb on irc.freenode.net
http://lists.pdxruby.org/mailman/listinfo/pdxruby
Duncan Beevers
2006-11-20 01:39:35 UTC
Permalink
A simple self-referential association is sufficient if you don't wish to
store any data about the relationship other than from-whom-to-whom. If, on
the other hand, you wish to track information about the relationship such as
when it was created or whether it is negative or positive, the relationship
should be promoted to a full-fledged model. In this case, a
has-many-through, where a user has many relationships, and many other users
through relationships, is appropriate.
Post by Dave Amos
Thanks, I just bought Rails Recipes and I guess I must've missed that
one. I'll go try it. Thanks!
Post by David Turnbull
You probably don't need the complexity of a :through. But you are
definitely looking at a self-referential. Here's the example from Rails
class Person < ActiveRecord::Base
has_and_belongs_to_many :friends,
:class_name => "Person",
:join_table => "friends_people",
:association_foreign_key => "friend_id",
:foreign_key => "person_id"
end
Don't forget to add a friends_people join table to the database. Then
you do controller stuff like: person1.friends << person2
Ignore that (confusing) blog post you were referred to and go buy Rails
Recipes if you need the rest of the details to make this work. Recipe 18
will cut and paste to exactly what you want to do.
Maybe get a new forums too.
-david
Hi there!
I'm working on a little recipe-sharing application, and I want people to
be able to click on a link in a user's profile to add them as "friends". I
imagined it working where once the link was clicked, the user_id of the
friendee and the current_user.id of the friender would be stored in a
database table called friends. I asked about this on a ruby forum, and most
people told me that the best way to do this was using self-referential
has_many :through associations (see: http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through
). They also concluded that it was well out of my league, and I guess I
agree. Is there a way to do it my way, without the associations (except for
maybe a simple users has_many friends sort of thing)?
Thanks, and let me know if you need more information!
Dave
_______________________________________________
PDXRuby mailing list
IRC: #pdx.rb on irc.freenode.net
http://lists.pdxruby.org/mailman/listinfo/pdxruby
_______________________________________________
PDXRuby mailing list
IRC: #pdx.rb on irc.freenode.net
http://lists.pdxruby.org/mailman/listinfo/pdxruby
_______________________________________________
PDXRuby mailing list
IRC: #pdx.rb on irc.freenode.net
http://lists.pdxruby.org/mailman/listinfo/pdxruby
Dave Amos
2006-12-10 00:05:44 UTC
Permalink
Hi David,

Thanks for the advice. I have Rails Recipes, and I must've missed that
one. I created the join table and added the code you suggested into the
model (I changed Person to User, since that's what I call users in my app,
so the table is friends_users, for example).

I believe that everything regarding the model and the database are set up
fine, but I don't really know how to create the friendships in my
controller. I created a link in each user profile that sends users to the
controller action 'friendify'. It looks like this:

def friendify
current_user.friends << @user
end

Current_user is my logged in user, and @user is the user current_user wants
to add as a friend (@user *should* be the user of the profile where I put
the "add as a friend" link).

Am I totally missing something? I get an error that says: User expected,
got NilClass. I'm not really sure what to do with it; I've never seen it
before.

Thanks!

Dave
Post by David Turnbull
You probably don't need the complexity of a :through. But you are
definitely looking at a self-referential. Here's the example from Rails
class Person < ActiveRecord::Base
has_and_belongs_to_many :friends,
:class_name => "Person",
:join_table => "friends_people",
:association_foreign_key => "friend_id",
:foreign_key => "person_id"
end
Don't forget to add a friends_people join table to the database. Then you
do controller stuff like: person1.friends << person2
Ignore that (confusing) blog post you were referred to and go buy Rails
Recipes if you need the rest of the details to make this work. Recipe 18
will cut and paste to exactly what you want to do.
Maybe get a new forums too.
-david
Hi there!
I'm working on a little recipe-sharing application, and I want people to
be able to click on a link in a user's profile to add them as "friends". I
imagined it working where once the link was clicked, the user_id of the
friendee and the current_user.id of the friender would be stored in a
database table called friends. I asked about this on a ruby forum, and most
people told me that the best way to do this was using self-referential
http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through).
They also concluded that it was well out of my league, and I guess I agree.
Is there a way to do it my way, without the associations (except for maybe a
simple users has_many friends sort of thing)?
Thanks, and let me know if you need more information!
Dave
_______________________________________________
PDXRuby mailing list
IRC: #pdx.rb on irc.freenode.net
http://lists.pdxruby.org/mailman/listinfo/pdxruby
_______________________________________________
PDXRuby mailing list
IRC: #pdx.rb on irc.freenode.net
http://lists.pdxruby.org/mailman/listinfo/pdxruby
Loading...