daniel
daniel

Reputation: 3115

Display the username in a view on a relation with 2 models

I got a User Model with 'username' and a Posts Model with 'user_id' as foreign key. How can I display the Username in a view?

/user.rb/
has_many :posts

/posts.rb/
belongs_to :user, :class_name => "User", :foreign_key => "user_id"

I want to use it in the 'show' method

Upvotes: 0

Views: 45

Answers (1)

Zabba
Zabba

Reputation: 65467

This should work:

<%= post.user.username %>

Upvotes: 1

Related Questions