Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 sort_by complications
    text
    copied!<p>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). 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.</p> <pre><code> &lt;% @section.topics.sort_by{ ?? }.each do |topic| %&gt; &lt;tr&gt; &lt;td class="topic"&gt;&lt;%= link_to topic.subject, [@section, topic] %&gt;&lt;/td&gt; &lt;td class="postCount"&gt;&lt;%= topic.replies.count %&gt;&lt;/td&gt; &lt;td class="date"&gt; &lt;% if topic.replies.empty? %&gt; &lt;%= topic.created_at.strftime("%I:%M%p %m/%d/%y") %&gt; &lt;% else %&gt; &lt;%= topic.replies.last.created_at.strftime("%I:%M%p %m/%d/%y") %&gt; &lt;% end %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; </code></pre> <p>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.</p> <p>I tried making a method in the topic model called "last_updated". I wrote it like:</p> <pre><code> def last_update(sectionTopic) if sectionTopic.replies.empty? return sectionTopic.created_at else return sectionTopic.replies.last.created_at end </code></pre> <p>end</p> <p>And passed it in through the view:</p> <pre><code>&lt;% @section.topics.sort_by{ |topic| topic.last_update(@section.topics)}.each do |topic| %&gt; </code></pre> <p>But I get a undefined method `replies' when I do this. Any ideas? Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload