dimanche 20 décembre 2015

Get model type from each loop

I'm combining two models in my controller, then trying to print them out in a similar fashion, but I'd like to do an if statement in the each loop to distinguish one model from the other.

My models are comments and likes.

In the controller:

@items = (@user.likes + @user.comments).sort{|a,b| a.created_at <=> b.created_at }

In my view:

<%= @items.each do |item| %>
  <% item.name %>
<% end %>

So basically, I need an if statement to say IF comment or IF like in the each loop. This is probably simple, but I've been drawing a blank on the situation.

Aucun commentaire:

Enregistrer un commentaire