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. I'm sure this is a simple 1 or 2-liner--any help?
<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