我正在尝试使用两个表中的数据对聊天框中的数据进行排序(有点想将事件记录在日志中)。
这是我的聊天框:
我想根据日期/时间对蓝色和绿色框进行排序。这是我要排序的代码:
您能指导我如何实现这一目标吗?
编辑 更新的代码:
stats.html.erb文件
<%
@objects = (@solrs + @messages).sort_by &:created_at
%>
<% @objects.each do |obj| %>
<% if obj.is_a? Solr %>
<!-- Solrs Partial -->
<%= render 'messages/solr' %>
<% else %>
<!-- Messages Partial -->
<%= render @messages %>
<% end %>
<% end %>
部分内部Solrs代码
<% @solrs.each do |solr| %>
<div class="row msg_container base_receive">
<div class="col-xs-10 col-md-10">
<div class="messages msg_receive">
<p class="">
Disease <b><%= solr.disease %></b> has been created with synonyms <b> <%= solr.synonym %></b> by <b><%= solr.user.first_name %> <%= solr.user.last_name %></b><br>
</p>
<time> <%= solr.created_at.strftime("%d %b %4Y")%> • <%=solr.created_at.strftime("%I:%M%p")%></time>
</div>
</div>
</div>
<% end %>
部分消息内部代码
<div class="row msg_container base_sent">
<div class="col-md-1 col-xs-1">
<p><em><%= message.user.first_name %>: </em></p>
</div>
<div class="col-md-10 col-xs-10">
<div class="messages msg_sent">
<p>
<%= message.body %>
</p>
<time> <%= message.created_at.strftime("%d %b %4Y")%> • <%=message.created_at.strftime("%I:%M%p")%></time>
</div>
</div>
</div>
solr_controller文件
def stats
@messages = Message.custom_display
@message = Message.new
@solrs = Solr.all
@users = User.all
end
答案 0 :(得分:0)
要将多个模型分类在一起,首先创建两个模型的集合:
@objects = @solrs + @messages
然后,使用[sort_by]
(https://apidock.com/ruby/Enumerable/sort_by):
@objects.sort_by &:created_at
-或-@objects.sort_by { |obj| obj.created_at }
非常确定您可以将其包装成一行,例如:
@objects = (@solrs + @messages).sort_by &:created_at
[is_a?]
(https://apidock.com/ruby/Object/is_a%3F)将让您分别对待每个人,this SO question goes over this nicely
solr_controller.rb
def stats
@messages = Message.includes(:user).custom_display
@solrs = Solr.includes(:user).all
@objects = (@solrs + @messages).sort_by &:created_at
end
[.includes]
(https://apidock.com/rails/ActiveRecord/QueryMethods/includes)允许您访问关联的用户,而无需再次查询数据库。 Here's a good article on it.
假设您在/ views / messages中,请调整文件结构是否不同
/messages/stats.html.erb
<% @objects.each do |obj| %>
<% if obj.is_a? Solr %>
<%= render partial: 'solr', locals: { solr: obj } %>
<% else %>
<%= render partial: 'message', locals: { message: obj } %>
<% end %>
<% end %>
/messages/_solr.html.erb
<div class="row msg_container base_receive">
<div class="col-xs-10 col-md-10">
<div class="messages msg_receive">
<p>
Disease <b> <%= solr.disease %> </b>
has been created with synonyms <b> <%= solr.synonym %> </b>
by <b> <%= solr.user.first_name %> <%= solr.user.last_name %> </b>
<br>
</p>
<time>
<%= solr.created_at.strftime("%d %b %4Y") %> •
<%= solr.created_at.strftime("%I:%M%p") %>
</time>
</div>
</div>
</div>
/messages/_message.html.erb
<div class="row msg_container base_sent">
<div class="col-md-1 col-xs-1">
<p> <em> <%= message.user.first_name %>: </em> </p>
</div>
<div class="col-md-10 col-xs-10">
<div class="messages msg_sent">
<p> <%= message.body %> </p>
<time>
<%= message.created_at.strftime("%d %b %4Y") %> •
<%= message.created_at.strftime("%I:%M%p") %>
</time>
</div>
</div>
</div>