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


On Nov 18, 2006, at 5:29 PM, Dave Amos wrote:

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
[email protected]
IRC: #pdx.rb on irc.freenode.net
http://lists.pdxruby.org/mailman/listinfo/pdxruby

_______________________________________________
PDXRuby mailing list
[email protected]
IRC: #pdx.rb on irc.freenode.net
http://lists.pdxruby.org/mailman/listinfo/pdxruby

Reply via email to