Showing a table for open and closed tickets This shows a table for open and closed tickets. The code for open tickets is identical with the closed tickets. I was trying to figure out a way to just write one code segment that will show both tables and use half of the code lines. With some changes I ended up to this code. With this code, if I iterate it twice and at at first use the word "open" and then rename it to "closed" then I will accomplish what I am after.

I looked around and figured out that I need to use a dynamic variable name. Something like sum#{status}tickets (pretty sure that something like that doesn't work) where the "status" variable will be at the first iteration "open" and then "closed" at the second one.

I was wondering if this is somehow possible (to iterate twice) and then dynamically set the variable's name.

   &lt;% if sumopentickets == 0 %&gt;
    &lt;h3&gt;Open Tickets&lt;/h3&gt;
    &lt;h4&gt;No open tickets.&lt;/h3&gt;
  &lt;% else %&gt;
   &lt;h3&gt;Open Tickets&lt;/h3&gt;
  &lt;table class="table"&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 colspan="2"&gt;Status&lt;/th&gt;
    &lt;/tr&gt;

    &lt;% @tickets.each do |ticket| %&gt;
    &lt;% if ticket.status == "open" %&gt;
    &lt;% user = User.find(ticket.user_id) %&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;%= user.username %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= ticket.created_at.strftime("%H:%M, %B %d, %Y") %&gt;&lt;/td&gt;
        &lt;% if ticket.ticketmessages.last &amp;&amp; ticket.ticketmessages.last.user_id %&gt;
        &lt;td&gt;(user waiting for reply...)&lt;/td&gt;
        &lt;% end %&gt;
      &lt;/tr&gt;
    &lt;% end %&gt;
    &lt;% end %&gt;
  &lt;/table&gt;
  &lt;% end %&gt;


  &lt;% if sumclosedtickets == 0 %&gt;
    &lt;h3&gt;Closed Tickets&lt;/h3&gt;
    &lt;h4&gt;No closed tickets.&lt;/h3&gt;
  &lt;% else %&gt;
  &lt;h3&gt;Closed Tickets&lt;/h3&gt;
  &lt;table class="table"&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 colspan="2"&gt;Status&lt;/th&gt;
    &lt;/tr&gt;

    &lt;% @tickets.each do |ticket| %&gt;
    &lt;% if ticket.status == "closed" %&gt;
      &lt;% user = User.find(ticket.user_id) %&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;%= user.username %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= ticket.created_at.strftime("%H:%M, %B %d, %Y") %&gt;&lt;/td&gt;
        &lt;% if ticket.ticketmessages.last &amp;&amp; ticket.ticketmessages.last.user_id %&gt;
        &lt;td&gt;(user waiting for reply...)&lt;/td&gt;
        &lt;% end %&gt;
      &lt;/tr&gt;
    &lt;% end %&gt;
    &lt;% end %&gt;
  &lt;/table&gt;
  &lt;% end %&gt;
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;