I’m currently rendering a JQuery Plugin on one of my partials in my Application layout. This partial calls one my application helper method which retrieves data generated using Sunspot-solr. The data it’s retrieving is the top 30 recently created records created across various models, Uploads, Users, Articles etc. I was wondering what the best practice would be for loading this data if it appears on every view in my application?
Obviously performance is my biggest concern.
This is how I’m currently retrieving the data in my Application helper:
def get_updates
@updates = Sunspot.search(Upload,Help,User) do
order_by(:created_at, :desc)
paginate page: 1, per_page: 30
end
@updates = @updates.results
end
You could fetch that data in a
before_filterin your ApplicationController. Then it would be available to any view rendered in any controller throughout the application.pseudo code:
Now you can use
@top_30in any view. The helper does not (and arguably should not) need to fetch the data since its already in an instance variable now, just pass it in.