I’m a noob so please forgive if this is an easy question but I’m trying to create a ‘Most Recently Popular’ output for specific content on a rails project.
Right now the object I am pulling from has an attribute revision.background_title. I want to calculate popularity by finding the number of specific background_title’s added over the past seven days then put them in order. For example if there are 4 background_title’s named ‘awesomecontent’ then that would be above one that has 1 background_title named ‘less awesome content’
This pulls all of them:
@revisions = Revision.find(:all, :order => “created_at desc”)
Thanks.
You can use the basic ActiveRecord
findmethod to do this. The code would end up looking something like this:To see the output, you could then do something like this:
giving
Another option would be to take a look at
ActiveRecord::Calculations:http://api.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html
Calculations supports a
countmethod that also supports thegroupoption. However, going this route will give you back a hash containing thebackground_titleas the key and thecountas the value. Personally, find the first method more useful.