# Foreign key confusion

**URL:** <https://www.ruby-forum.com/t/foreign-key-confusion/62848>\
**Category:** Rails\
**Created:** [June 8, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/foreign-key-confusion/62848 "2006-06-08T18:56:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 8, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/foreign-key-confusion/62848/1 "2006-06-08T18:56:24Z")

</div>

I have 2 models right now: “users” and “posts”. When a user creates a  
post I would like their id to be recorded under “user\_id” in the “posts”  
table. Under the “user” model I have:

class User \< ActiveRecord::Base  
has\_many :posts  
end

And under the “posts” model I have:

class Post \< ActiveRecord::Base  
belongs\_to :user  
end

When I create a post everything goes fine, but the “user\_id” field in  
“posts” is left at 0. I know I’m missing something, but I can’t seem to  
translate all the documentation out there into my scenario. Any help  
would be MUCH appreciated.

---

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 8, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/foreign-key-confusion/62848/2 "2006-06-08T19:06:45Z")

</div>

Jasbur wrote:

> class Post \< ActiveRecord::Base  
> belongs\_to :user  
> end
> 
> When I create a post everything goes fine, but the “user\_id” field in  
> “posts” is left at 0. I know I’m missing something, but I can’t seem to  
> translate all the documentation out there into my scenario. Any help  
> would be MUCH appreciated.

hm, how are you taking what’s in @session[:user].id and putting it in  
your post ?  
in the controller, when you’re creating the post, you could do :

@post = Post.new(@params[:post\_formstuff])  
@post.user\_id = @session[:user].id  
@post.save

Also, something that may be wrong too, i tend to explicitly mention the  
foreign\_key in the model.

belongs\_to :user, :foreign\_key =\> “user\_id”

that may work, however its early for me and i need coffee.

d.

---

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 8, 2006, 7:16pm UTC](https://www.ruby-forum.com/t/foreign-key-confusion/62848/3 "2006-06-08T19:16:42Z")

</div>

Well, even without your coffee you scooled me 🙂 I did have to change:

@post.user\_id = @session[:user].id

to

@post.user\_id = @session[‘user’].id

for some reason. But it works great. I was looking at the active record  
stuff as more of a “write” function than a “read” function. I thought it  
would figure the syntax of the session itself.

Thanks again

---

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 8, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/foreign-key-confusion/62848/4 "2006-06-08T19:22:52Z")

</div>

you’re not confused  
well, it kinda depends on how you created your post  
what your doing is correct, look

use script/console

> > require ‘pp’ #=\> just for pretty printing

> > rodney = User.create(:name =\> ‘rodney’)

> > pp rodney  
> > #\<User:0x332dcfc  
> > @attributes={“name”=\>“rodney”, “id”=\>1},

[snip]

> > post = Post.create(:title =\> ‘The World is all that is the case’,  
> > :body =\> ‘Too Wittgy’, :user =\> rodney )

> > pp post

> > pp post  
> > #\<Post:0x32187e0  
> > @attributes=  
> > {“body”=\>“Too wittgy”,  
> > “title”=\>“The World is all that is the case”,  
> > “id”=\>1,  
> > “user\_id”=\>1},  
> > [snip]

your post has user\_id set to 1

hth

/r/r

On 6/8/06, Jasbur [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Post \< ActiveRecord::Base
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Rodney  
[http://www.pinupgeek.com](http://www.pinupgeek.com)  
[http://www.dutchrailers.org](http://www.dutchrailers.org)
