Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7614271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:21:43+00:00 2026-05-31T02:21:43+00:00

I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to

  • 0

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
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :avatar, :subscribed_tag_names

  has_many :posts, :dependent => :destroy
  has_many :comments, :dependent => :destroy
end

and a 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 post 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 the post (as you can see in StackOverflow and various forums)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:21:44+00:00Added an answer on May 31, 2026 at 2:21 am

    The previous answer only works when your post has some comments. When there are no comments then you will receive an error, as you have noted.

    You could solve this by simply testing that comments exist before trying to output it:

    <% @posts.each do |post| %>
      ...
    
      <% if post.comments.empty? %>
        Nobody has commented yet
      <% else %>
        <%= post.comments.last.user.email %>
      <% end %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to
I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to
I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names has_many
I have the following models: class EventParticipant < ActiveRecord::Base belongs_to :event has_one :user attr_accessible
I have a Tag model: class Tag < ActiveRecord::Base has_many :taggings, :dependent => :destroy
I have the model Post: class Post < ActiveRecord::Base has_one :location, :dependent => :destroy
I have a typical, Post model: class Post< ActiveRecord::Base validates_presence_of :user_id #Line 1 validates_presence_of
I have the following Models: class Topic < ActiveRecord::Base has_many :posts, :dependent => :destroy
I have a User model, a Post model, and an Interest model. User has_many
I have a model as follows: class EntityTag < ActiveRecord::Base attr_protected :user_id, :post_id, :entity_id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.