Simple noob question about rails data access


#1

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…


#2

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


#3

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


#4

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?


#5

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!