# Simple noob question about rails data access

**URL:** <https://www.ruby-forum.com/t/simple-noob-question-about-rails-data-access/109871>\
**Category:** Rails\
**Created:** [July 19, 2007, 7:42pm UTC](https://www.ruby-forum.com/t/simple-noob-question-about-rails-data-access/109871 "2007-07-19T19:42:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 19, 2007, 7:42pm UTC](https://www.ruby-forum.com/t/simple-noob-question-about-rails-data-access/109871/1 "2007-07-19T19:42:29Z")

</div>

One of those things where i feel i should know how to do this, but just  
can’t work it out (maybe it’s time to stop working!)

I have an ‘article’ object which has a foreign key to a ‘user’, linked  
by ‘user\_id’. My current article on a view page is called @article.  
What i need to do is to get the ‘login’ attribute of the user with the  
same id as @article 's user\_id.

Ie - if i know a user id, how do i get the user object with that id?

Like i say i know this is simple but i’m having a brain stoppage…

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 19, 2007, 7:52pm UTC](https://www.ruby-forum.com/t/simple-noob-question-about-rails-data-access/109871/2 "2007-07-19T19:52:31Z")

</div>

First of all make sure you setup your relationships in you models  
correctly. There are several ways to get the user id and it depends  
on the context that you need to find the id. You can grab a user based  
off of an id by User.find(:params[id]), this will return a user object  
with the given id. Hope this points you in the right direction.

Wes

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 19, 2007, 7:54pm UTC](https://www.ruby-forum.com/t/simple-noob-question-about-rails-data-access/109871/3 "2007-07-19T19:54:32Z")

</div>

Hi Max,

Max W. wrote:

> I have an ‘article’ object which has a foreign key to a ‘user’, linked  
> by ‘user\_id’. My current article on a view page is called @article.  
> What i need to do is to get the ‘login’ attribute of the user with the  
> same id as @article 's user\_id.
> 
> Ie - if i know a user id, how do i get the user object with that id?

Assuming that your User model has\_many :articles, and your Article model  
belongs\_to :user, you can access that attribute in your view with:

@article.user.login

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 19, 2007, 8:00pm UTC](https://www.ruby-forum.com/t/simple-noob-question-about-rails-data-access/109871/4 "2007-07-19T20:00:37Z")

</div>

Acroterra wrote:  
You can grab a user based off of an id by User.find(:params[id]),

Bill wrote:  
Assuming that your User model has\_many :articles, and your Article model  
belongs\_to :user, you can access that attribute in your view with:

@article.user.login

Thanks, but neither of these work for me!  
User.find(:params[id])  
looks for a user with the same id as the current article’s id, not the  
current article’s user\_id.

@article.user.login  
returns nil, even though there is definitely a user with the same id as  
the article’s user\_id (i checked in the database gui). I have set up  
both models with the correct relationships, as you said.

Both of these are good methods i’m sure, but for some reason are doing  
the wrong things for me. Thanks for the advice, any idea what might be  
going wrong?

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 19, 2007, 8:01pm UTC](https://www.ruby-forum.com/t/simple-noob-question-about-rails-data-access/109871/5 "2007-07-19T20:01:51Z")

</div>

Max W. wrote:

> Acroterra wrote:  
> You can grab a user based off of an id by User.find(:params[id]),
> 
> Bill wrote:  
> Assuming that your User model has\_many :articles, and your Article model  
> belongs\_to :user, you can access that attribute in your view with:
> 
> @article.user.login
> 
> Thanks, but neither of these work for me!  
> User.find(:params[id])  
> looks for a user with the same id as the current article’s id, not the  
> current article’s user\_id.
> 
> @article.user.login  
> returns nil, even though there is definitely a user with the same id as  
> the article’s user\_id (i checked in the database gui). I have set up  
> both models with the correct relationships, as you said.
> 
> Both of these are good methods i’m sure, but for some reason are doing  
> the wrong things for me. Thanks for the advice, any idea what might be  
> going wrong?

DOH, sorry bill, i’m an idiot. I wrote @Article… instead of  
@article…

It’s working great now. Thanks!
