I have two different kinds of conversations, started and joined. I want to render them together in the same stream from newest on top to oldest on bottom. That is, I want them to be intermixed so that the newest conversation is on top regardless of whether it was started by the user or joined by the user (i.e. started by another user).
From my controller:
def conversations
@user = current_user
@joined_conversations = @user.received_meeting_requests.paginate(page: params[:page], :conditions => ['state = ?', 'replied'])
@started_conversations = @user.sent_meeting_requests.paginate(page: params[:page], :conditions => ['state = ?', 'replied'])
end
In my view, I am rendering like this:
<ol class="meetings">
<%= render @started_conversations %>
<%= render @joined_conversations %>
</ol>
<%= will_paginate @started_conversations %>
<%= will_paginate @joined_conversations %>
<% end %>
So even the oldest started conversation will be above the newest joined conversation. How to I render these two objects so that they are intermixed, i.e. the newest started OR joined conversation will always be on top? Thanks!
Well, there are a few ways to actually go about it, but what you need is to have both kinds of conversations in one collection.
Now, you could just take both your existing collections, put them together (using the Array union operator ‘|’), and resort them:
However, since this sort of filtering and sorting is what databases are really for, I think you would do better to collect all the conversations that are relevant in one query. I’d like to tell you what that query is, but there’s no way to say without knowing about what your models/tables are, how they’re associated, what it means for a user to have joined or started a conversation, and for that matter what version of Rails and what database you’re using, since it will look quite a bit different in Rails 3.x vs 2.x.
In either case, you’ll end up with one collection called
@conversations, and should be able to simply render that.