0

I have two content types, articles and events. I want to create a partial on my homepage to serve an array of both.

I was thinking of creating a helper method called latest_news, something like this

def latest_news(limit=12)
    @articles = Article.all
    @events = Event.all
    sort_by(&:created_at)
end

Also, how would I sort the articles by their publish_date and the events by their event_date?

1 Answers1

0

Why do you want to put it in helper method? You can have @articles and @events in your controller action which is serving home page.

Also you can sort individually both the content

@articles = Article.order(:publish_date)

@events = Event.order(:event_date)

Icicle
  • 1,174
  • 9
  • 13
  • I'm not sure I follow your suggestion - do you mean to say put this in a defined method inside the homepage controller? – Arash Hadipanah Jul 16 '13 at 19:58
  • Yes.. I understand from your post that you want to show articles and events in your home page so whatever action which is defined for home page can execute above queries – Icicle Jul 16 '13 at 20:00
  • How would you handle the array in the partial to show things between the two content types? For instance, events have a location where articles do not – Arash Hadipanah Jul 16 '13 at 20:12
  • I assume that you are having separate partials for both events and articles so you will be using relevant objects and details accordingly Check this out http://rails-bestpractices.com/posts/61-simplify-render-in-views http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials – Icicle Jul 16 '13 at 20:16
  • No, I have a single partial for both. I want to have an array that returns both articles and events within that partial. – Arash Hadipanah Jul 16 '13 at 20:21