# Habtm foreign key must be manually done

**URL:** <https://www.ruby-forum.com/t/habtm-foreign-key-must-be-manually-done/104967>\
**Category:** Rails\
**Created:** [May 24, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/habtm-foreign-key-must-be-manually-done/104967 "2007-05-24T12:23:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [May 24, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/habtm-foreign-key-must-be-manually-done/104967/1 "2007-05-24T12:23:04Z")

</div>

I have a habtm relationship between User and Activity

when I create a new Activity why do I have to manually set the foreign  
key  
user\_id in the activity table???

example

def create  
@activity = Activity.new(params[:activity])  
@activity.user\_id = self.current\_user.id #why is this  
necessary??? but I guess rails can’t magically know what I mean by  
user???  
can it?

if @activity.save  
@activity.users \<\< User.find\_by\_id(self.current\_user.id)  
#habtm  
end

end

cheer

dion

---

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [May 25, 2007, 11:29am UTC](https://www.ruby-forum.com/t/habtm-foreign-key-must-be-manually-done/104967/2 "2007-05-25T11:29:14Z")

</div>

Hi Dion,

I’m a little confused by your example. Why does Activity have a  
user\_id on it if you have a HABTM relationship? There should be a  
join table named “activities\_users” that has only two columns,  
“activity\_id” and “user\_id”, and you shouldn’t need a user\_id column  
on Activity.

This line:

```
 @activity.users << User.find_by_id(self.current_user.id ) #habtm

```

should also probably just be

```
 @activity.users << current_user

```

to save yourself some database hits.

Regards,

-Seth

---

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [May 25, 2007, 2:44pm UTC](https://www.ruby-forum.com/t/habtm-foreign-key-must-be-manually-done/104967/3 "2007-05-25T14:44:42Z")

</div>

On 5/24/07, Dion H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a habtm relationship between User and Activity
> 
> when I create a new Activity why do I have to manually set the foreign key  
> user\_id in the activity table???

You don’t have to. The habtm relationship should set everything up  
automatically.

> ```
> @activity.users << User.find_by_id(self.current_user.id ) #habtm
> 
> ```
> 
> end
> 
> end

You’re forgetting that @activity is just an AR object, not a database  
entry. In order to propagate the changes in the database you should  
call the save method:

@activity.users \<\< User.find\_by\_id(self.current\_user.id )  
@activity.save

–  
Andrei M.  
[http://andreimaxim.ro](http://andreimaxim.ro)

---

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [May 25, 2007, 2:56pm UTC](https://www.ruby-forum.com/t/habtm-foreign-key-must-be-manually-done/104967/4 "2007-05-25T14:56:43Z")

</div>

@activity.users \<\< current\_user

does the trick, cheers

still curbing my old school thinking

---

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [May 25, 2007, 2:47pm UTC](https://www.ruby-forum.com/t/habtm-foreign-key-must-be-manually-done/104967/5 "2007-05-25T14:47:42Z")

</div>

On 5/25/07, Andrei M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
…

> You’re forgetting that @activity is just an AR object, not a database  
> entry. In order to propagate the changes in the database you should  
> call the save method:
> 
> @activity.users \<\< User.find\_by\_id(self.current\_user.id )  
> @activity.save

Actually you should try

@activity.users \<\< current\_user  
@activity.save

because you won’t have to do an extra query.

–  
Andrei M.  
[http://andreimaxim.ro](http://andreimaxim.ro)
