# Click a link and insert into a database?

**URL:** <https://www.ruby-forum.com/t/click-a-link-and-insert-into-a-database/75883>\
**Category:** Rails\
**Created:** [November 14, 2006, 12:28am UTC](https://www.ruby-forum.com/t/click-a-link-and-insert-into-a-database/75883 "2006-11-14T00:28:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 14, 2006, 12:28am UTC](https://www.ruby-forum.com/t/click-a-link-and-insert-into-a-database/75883/1 "2006-11-14T00:28:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 14, 2006, 1:16am UTC](https://www.ruby-forum.com/t/click-a-link-and-insert-into-a-database/75883/2 "2006-11-14T01:16:12Z")

</div>

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](http://www.railsweenie.com/forums/2/topics/768).

c.

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 14, 2006, 1:31am UTC](https://www.ruby-forum.com/t/click-a-link-and-insert-into-a-database/75883/3 "2006-11-14T01:31:53Z")

</div>

> 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](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!

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 14, 2006, 5:06am UTC](https://www.ruby-forum.com/t/click-a-link-and-insert-into-a-database/75883/4 "2006-11-14T05:06:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [November 14, 2006, 10:36am UTC](https://www.ruby-forum.com/t/click-a-link-and-insert-into-a-database/75883/5 "2006-11-14T10:36:00Z")

</div>

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](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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
