Showing a table for open and closed tickets
I'm going to assume that this is in fact a ruby-on-rails project - not just plain Ruby.

In that case, long story short: Stop doing everything in the view. Use Rails and its conventions.

In your model, add a proper relationship to the user, and a couple of scopes

belongs_to :user

scope :open, where(status: :open)
scope :closed, where(status: :closed)


Also add a method

def waiting_for_reply?
  # I don't quite understand your logic (or your naming) here,
  # so I'll just shorten it, but otherwise leave it
  ticketmessages.last.try(:user_id).present?
end


I say I don't understand your logic, because something called ticketmessages implies there's a class called Ticketmessage - when it should be called TicketMessage as it's two words (or simply Message). And in that case, the relation should be ticket_messages with an underscore (or, again, simply messages).
Secondly, you check whether there is a (last) ticket, and whether its user_id is non-nil/non-false. But what does that prove? If there's any system to how it's set up, a message will always have a user_id because every message was written by a user. The real question (I assume) is whether that user_id is equal to the ticket's user_id - otherwise someone else wrote it. But that's not what you're checking, so I'm confused.

Anyway, in your controller, load two collections (and eagerly load the associated users):

@open_tickets = Ticket.open.includes(:user)
@closed_tickets = Ticket.closed.includes(:user)


In your view, extract the table itself into a partial, and only include HTML for the parts that actually differ. Note the call to render where we point it to the partial template it should use, and the local variables that should be made available to that template.

&lt;h3&gt;Open Tickets&lt;/h3&gt;
&lt;% if @open_tickets.any? %&gt;
  &lt;%= render partial: "tickets", locals: { tickets: @open_tickets } %&gt;
&lt;% else %&gt;
  &lt;h4&gt;No open tickets&lt;/h4&gt; &lt;!-- this should just be a P element by the way --&gt;
&lt;% end %&gt;

&lt;h3&gt;Closed Tickets&lt;/h3&gt;
&lt;% if @closed_tickets.any? %&gt;
  &lt;%= render partial: "tickets", locals: { tickets: @closed_tickets } %&gt;
&lt;% else %&gt;
  &lt;h4&gt;No closed tickets&lt;/h4&gt; &lt;!-- see above --&gt;
&lt;% end %&gt;


And here's the partial

&lt;table class="table"&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Ticket Number&lt;/th&gt;
      &lt;th&gt;Title&lt;/th&gt;
      &lt;th&gt;Submitted by&lt;/th&gt;
      &lt;th&gt;Created at&lt;/th&gt;
      &lt;th&gt;Status&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;

  &lt;tbody&gt;
  &lt;% tickets.each do |ticket| %&gt; &lt;!-- here we use the local "tickets" --&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;%= ticket.id %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= link_to ticket.title, ticket_path(ticket.url_token) %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= ticket.user.username %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= ticket.created_at.strftime("%H:%M, %B %d, %Y") %&gt;&lt;/td&gt; &lt;!-- this formatting stuff should be a helper method --&gt;
      &lt;td&gt;
      &lt;% if ticket.waiting_for_reply? %&gt;
        (user waiting for reply...)
      &lt;% end %&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;% end %&gt;
  &lt;/tbody&gt;
&lt;/table&gt;