Forum: Ruby on Rails Simple noob question about rails data access

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2007-07-19 19:42
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...
9805c4a69c33de97b16249586b47205b?d=identicon&s=25 Acroterra (Guest)
on 2007-07-19 19:52
(Received via mailing list)
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
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-07-19 19:54
(Received via mailing list)
Hi Max,

Max Williams 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
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2007-07-19 20:00
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?
73c04e9ef9ca435c5b19a2e765ae6d20?d=identicon&s=25 Max Williams (max-williams)
on 2007-07-19 20:01
Max Williams 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!
This topic is locked and can not be replied to.