Hi, I''m trying to create a little system where users can add each other as "friends". To do this, I''m imagining one user going to the profile of another user and clicking a link that says "Add this person as a friend". When the link is clicked, I''d like the username of both of the users to be entered into a database table called "friends". I just don''t know where to start with this (except that I set up the table). The session data for the current user is aptly called "current_user", so somehow that data, plus the username of the profile being viewed needs to be added as a row in the friends table. I''m really new to this, but I know that I probably need to create a controller for friends, and set up an action like "Add". I just don''t know how to insert those two usernames into the table properly. Any resources on this topic? Thanks in advance. -- 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 -~----------~----~----~----~------~----~------~--~---
Dave A. wrote:> Hi, I''m trying to create a little system where users can add each other > as "friends". To do this, I''m imagining one user going to the profile > of another user and clicking a link that says "Add this person as a > friend". When the link is clicked, I''d like the username of both of the > users to be entered into a database table called "friends". > > I just don''t know where to start with this (except that I set up the > table). The session data for the current user is aptly called > "current_user", so somehow that data, plus the username of the profile > being viewed needs to be added as a row in the friends table. > > I''m really new to this, but I know that I probably need to create a > controller for friends, and set up an action like "Add". I just don''t > know how to insert those two usernames into the table properly. > > Any resources on this topic? Thanks in advance.First off - it sounds like you are biting off a lot more than you can chew right now. I''d really recommend getting Agile Web Development With Rails book and going through the tutorial/sample so that you can get a feel for how a rails app gets going start-to-(sort of)-finish. Sort of get some stuff under your belt before you dive in on this, I mean, because though it sounds simple you are getting into things with names like "self-referential" and "many-to-many" and "association callbacks", etc. that sort of drop into the second level of complexity in RoR. In the meantime - what you want to do is a self-referential many-to-many join. You can google for this and find it explained in many places, like here: http://www.railsweenie.com/forums/2/topics/768. c. -- 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 -~----------~----~----~----~------~----~------~--~---
> First off - it sounds like you are biting off a lot more than you can > chew right now. I''d really recommend getting Agile Web Development With > Rails book and going through the tutorial/sample so that you can get a > feel for how a rails app gets going start-to-(sort of)-finish. Sort of > get some stuff under your belt before you dive in on this, I mean, > because though it sounds simple you are getting into things with names > like "self-referential" and "many-to-many" and "association callbacks", > etc. that sort of drop into the second level of complexity in RoR. > > In the meantime - what you want to do is a self-referential many-to-many > join. You can google for this and find it explained in many places, like > here: http://www.railsweenie.com/forums/2/topics/768. > > c.Oh, I didn''t realize it was so complicated! I thought that setting up a separate database table that just dropped in a couple of values when you click a link was pretty easy, but not obvious to me. I''ve got a copy of Agile and I''ll do some more looking online. Thanks! -- 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 -~----------~----~----~----~------~----~------~--~---
Cayce is right you need that book. the view code you''ll want is <%= link_to ''Add Friend'' [ :controller => ''friends'', :action => ''add'', :id => @user_id ] %> and the code for add would look something like class FriendsController def add if params[:id] connector = UserFriends.new connector.user_id = session[:user_id].id connector.friend_id = params[:id] connector.save end end end -- 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 -~----------~----~----~----~------~----~------~--~---
Ya setting it up properly is a bit complex for new rails programmers. Here''s another really good source: http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through. Scroll down to see a "friends" example in the user comments. Basically you''ve got a user table (nodes) and are connecting users inside that table with each other through "friendships" (edges). Cheers, Chad On Nov 13, 8:06 pm, Keynan Pratt <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Cayce is right you need that book. > > the view code you''ll want is > > <%= link_to ''Add Friend'' [ :controller => ''friends'', :action => ''add'', > :id => @user_id ] %> > > and the code for add would look something like > > class FriendsController > > def add > if params[:id] > connector = UserFriends.new > connector.user_id = session[:user_id].id > connector.friend_id = params[:id] > connector.save > end > end > > end > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---