Click a link and insert into a database?

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.

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 D. 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.

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 D. 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!

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

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 P. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs