简体   繁体   English

Rails 3 sort_by并发症

[英]Rails 3 sort_by complications

So I have a forum application and double nested resources. 所以我有一个论坛应用程序和双重嵌套资源。 Section-->Topic-->Replies and are set up as such in the application controller (Rails 3 style). 部分->主题->答复,并在应用程序控制器中进行了这样的设置(Rails 3样式)。 I'm looking at the section view and am trying to sort my topics. 我正在查看剖面图,并试图对主题进行排序。 In most forums the most logical sorting method is by when it's updated last. 在大多数论坛中,最合乎逻辑的排序方法是根据上次更新的时间。 This would be simple if it was just by replies. 如果只是通过答复,这将很简单。 The obvious problem is, there are topics without replies, so sorting simply by replies won't work. 明显的问题是,有些主题没有答复,因此仅按答复进行排序是行不通的。

            <% @section.topics.sort_by{ ?? }.each do |topic| %>
              <tr>
                <td class="topic"><%= link_to topic.subject, [@section, topic] %></td>
                <td class="postCount"><%= topic.replies.count %></td>
                <td class="date">
                <% if topic.replies.empty? %>
                  <%= topic.created_at.strftime("%I:%M%p %m/%d/%y") %>
                <% else %>
                  <%= topic.replies.last.created_at.strftime("%I:%M%p %m/%d/%y") %>
                <% end %>
                </td>
              </tr>  
            <% end %>

So basically if the replies are empty, you pull when the topic was created. 因此,基本上,如果答复为空,则在创建主题时拉出。 If there are replies, pull when the last one was created. 如果有答复,请在创建最后一个答复时拉出。 This displays wonderfully in the show sections view, but I have no clue how to sort using this. 这样可以在“显示部分”视图中很好地显示,但是我不知道如何使用它进行排序。

I tried making a method in the topic model called "last_updated". 我尝试在主题模型中创建一个名为“ last_updated”的方法。 I wrote it like: 我这样写:

  def last_update(sectionTopic)
if sectionTopic.replies.empty?
  return sectionTopic.created_at
else
  return sectionTopic.replies.last.created_at
end

end 结束

And passed it in through the view: 并通过视图传递了它:

<% @section.topics.sort_by{ |topic| topic.last_update(@section.topics)}.each do |topic| %>

But I get a undefined method `replies' when I do this. 但是当我这样做时,我得到一个未定义的方法“答复”。 Any ideas? 有任何想法吗? Thank you. 谢谢。

From what I can tell, 据我所知

topic.last_update(@section.topics)

should be: 应该:

topic.last_update(topic)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM