I’m trying to get a “What’s new” section working in my Rails app that takes into account new records created for various tables that don’t share any relationships. The one thing they do have in common is that they all have a created_at field, which I’m going to use to determine if they’re indeed “new” and then I’m wanting to sort the results by that common field. I tried doing this with Sunspot, but I couldn’t figure out how to make use of the the result set returned from the Sunspot search…
For instance in my Uploads and Article models I have:
searchable do
time :created_at
end
and in my search action I’ll do this:
@updates = Sunspot.search(Upload,Article) do
with(:created_at).greater_than(1.hour.ago)
end
Which does seem to return something, if I do an @updates.total it returns the number of records I was expecting to find. Beyond this I’m not sure how to actually make use of the records. What I’d like to do is send @updates to a view and determine the model type of each record and then proceed to print out the relevant information, i.e names, descriptions, parent/child record information (for instance upload.user.username).
I might be going at this all wrong, perhaps there’s a better option than sunspot for the simple search I’m attempting to perform?
Refer readme for details of how to use the search results. The method you are looking for is “results”, which will give you first 30 results, by default: