Tuesday, 15 July 2014

Sort two set of instances based on one of them - Views - Ruby on Rails -


ROR app contains posts and categories, main line categories / posts have a line set of this category per category (3 days) More new). Empty categories are not shown

I want to sort the index view to show the categories with the most recent posts, so when a new post is created for a category, then the category app's first "Row" goes up within a category, posts should also be ordered from new to large.

So I need to sort the two levels, the categories are categories with categories along with the latest categories and the category of posts according to the category. I'm really stuck, how do you implement it?

My Controller:

  DIF Index = Segment = Category. In White (: Post). Selection {| C | C.posts.where (['created_at & gt ;?', 3.days.ago]). Count & gt; 0} end  

My view:

  & lt;% @ categories.each do | Category | & Gt%; # Category Show & lt;% category.posts.where (['created_at>', ', 3.days.ago]) Do each. Posted in | & Gt%; #Show Post Title and Date & lt;% end% & gt; & Lt;% end% & gt;   

Update: To show what I've tried so far:

I posted the first categories, based on the post Tried to sort 'creation_data (as explained in the category, post is: post). In my controller:

  @segment = category. Include (: post) .dr ('posts.created_at'). {| C | C.posts.where (['created_at & gt ;?', 3.days.ago]). Count & gt; 0}  

Then, in my opinion, organize the loaded posts:

  <% categories.each do | Category | & Gt%; # Category display name & lt;% category.posts.order ('created_at'). Where (['created_at & gt ;?', 3.days.ago]). Every do Post | & Gt%; #Show Post Title and Date & lt;% end% & gt; & Lt;% end% & gt;  

with respect to counter_cache sugestion; I understand that this database will be for the optimization of the call, although not strictly necessary.

Solution : As stated in the previous update; But with the change of command ('posts.created_at') per command ('posts.created_at DESC') in the controller; And changes of order ('created_at') copy command ('created DASC') in the view.

Can you try the following? (Sorry for gets , but it will be faster)

  class Jens (: post). Selection ('Categories. *'). Groups ('Categories ID'). Where ('posts.created_at & gt ;?', 30.days.ago). Howing ('COUNT (पद. *) & Gt;) .डर (' posts.created_at DESC ').  

No comments:

Post a Comment