Reputation: 273
I have an situation where I want to show an each loop of 'updates'. Each update had a game associated with it (game_id). Some times there are updates that have the same game as other updates. How do I modify the loop so that it only shows unique updates? (in the example code below, I don't want to show the same game thumbnail twice)
In the view:
<% current_user.updates.each do |update| %>
<img src="<%= update.game.thumbnail %>" />
<% end %>
In the updates model
belongs_to :user
belongs_to :game
In the game model
has_many :users
has_many :updates
In the user model
has_many :updates
Upvotes: 0
Views: 623
Reputation: 7339
There are probably a half a dozen ways to do this. I would probably use a scope on the update model something like:
scope :most_recent, group(:game_id).order('created_at ASC')
This will give you the most recent entry by date for each game id.
Then in your view
<% current_user.updates.most_recent.each do |update| %>
Upvotes: 1