I have a Post model:
class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to :user has_many :comments, :dependent => :destroy end belongs_to :post, :counter_cache => true belongs_to :user end
a User Model:
class User < ActiveRecord::Base
and comment model:
class Comment < ActiveRecord::Base attr_accessible :content, :user_id belongs_to :post, :counter_cache => true belongs_to :user end
This is how I show the user who created the message in the index.html.erb view:
<% @posts.each do |post| %> <div id="post-<%= post.id %>" class="post"> <h3 class="post-title"><%= link_to post.title, post %></h3> <div class="post-author"> <span class="profile-picture"> <%= image_tag post.user.avatar.url(:thumb) %> </span> <span class="post-author-name"> <strong><%= link_to post.user.username, post.user %></strong> </span> </div>
(etc....)
How to display the user who last commented on the message (as you can see in StackOverflow and in different forums)?
source share