<james@...> writes:
>
>
> Greetings list,
> I have a rather simple app with posts and comments. When a user submits
> a comment (they must login), I want to display that user''s
username. I
> have a field in the "comments" table for user_id, and I can get
the
> page to show the id, but I need to take it a step farther (because the
> id means nothing to the public) and show the username associated with
> the id.
if you have a
class Comment < ActiveRecord::Base
belongs_to :user
end
then you can just call @comment.user.name (assuming that .name is a valid method
for User)
Alternatively, if you have
class User < ActiveRecord::Base
def to_s
name
end
end
then you can just call @comment.user somewhere that expects a String and
it''ll
call .name automatically
Gareth
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---