I have a page that has several “Feeds”. In my controller I have something like this:
def index
@plays = current_user.plays.includes(:game).order("created_at desc")
@wants = current_user.wants.includes(:game).order("created_at desc")
@ratings = current_user.ratings.includes(:game).order("created_at desc")
end
and I use code like this in the view:
<% @plays.each do |play| %>
You played <%= play.game.name %></p>
<% end %>
Now I want to make a forth feed on the page that is “all activity” which displays Plays, Wants and Ratings in one feed sorted by created date (desc).
Any help on the best way to do this would be great.
UPDATE: As requested code from each model, nice and simple right now:
class Play < ActiveRecord::Base
attr_accessible :game_id, :user_id, :description
belongs_to :user
belongs_to :game
end
class Rating < ActiveRecord::Base
attr_accessible :game_id, :id, :score, :user_id, :description
belongs_to :user
belongs_to :game
end
class Want < ActiveRecord::Base
attr_accessible :game_id, :user_id, :description
belongs_to :user
belongs_to :game
end
Since these come from separate tables, you won’t be able to sort them in the database query. But since you already have each of the arrays, you can use Ruby to combine and sort: