fuskie
fuskie

Reputation: 85

How do i get User email in posts view

This is my posts#index so far:

<% @posts.each do |post| %>
    <%= post.title %><br>
    <%= post.body %> <br>
    <b>Author:</b> <%= post.user_id %>
<% end %>

I'd like to put user.email there instead of id. How can i do that? Thanks.

Upvotes: 0

Views: 50

Answers (1)

Juan Guerrero
Juan Guerrero

Reputation: 613

Please have a look at Rails docs for model associations.

Long story short, you should have a relationship between post and user (a users have many posts) and get the email like post.user.email

Upvotes: 1

Related Questions