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... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Max Williams wrote:> 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... > > -- > Posted via http://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---