SQLite, Ruby on rails. How to get the top voted objects with acts_as_votable gem?

I"m trying to display the best 3 Recipes from the last 7 days. The best is dependant on the amount of votes it has using the acts_as_votable gem but i have not created a cache. current user.rb, limits to the last 7 days

def featuredfeed Recipe.where('created_at >= ?', Time.zone.now - 1.week) end


class Recipe < ActiveRecord::Base belongs_to :user acts_as_votable .... default_scope -> { order(created_at: :desc) } end

the votes table is ordered

id, votable_id, votable_type, voter_id, voter_type, vote_flag, vote_scope, vote_weight, created_at, updated_at

The votable_id is the id of the recipe which needs to be counted for the number of times it has been upvoted


I would like to say, that caching votes is much cleaner solution, than the following one. It is just a matter of adding a new migration. But up to you..

Get the ids of recent recipes (this part you already have done):

recipes_ids = Recipe.where('created_at >= ?', Time.zone.now - 1.week)

Filter the votes for this recipes:

Vote .where(votable_id: recipes_ids)

Group them by votable_id:


And find those, which have more, at least 1 vote:

.having('count(votable_id) > 1')

Now take the ids of 3 most upvoted recipes:

most_voted_recipes_ids = Vote .where(votable_id: recipes_ids) .group(:votable_id) .having('count(votable_id) > 1') .limit(3).votable_ids # or limit(3).pluck(:votable_id)

And, lastly, find those most popular recipes:

most_popular_recipes = Recipe.where(id: most_voted_recipes_ids)


  • Payola issue: undefined method `amount' for nil:NilClass
  • index method in controller not working for active model serializer
  • Saving Entries to Database Relationship (many-to-many) Table from a Backbone Project
  • Can't save Image to database using CarrierWave Direct
  • JOOQ forced type code generation
  • function cannot execute on segment because it accesses relation
  • AngularJs ng-repeat filtering by a deeper tier of data
  • Google Cloud SQL and time zones
  • Rails CarrierWave versions are not created for some reason
  • Is it possible to disable certain weekdays in DatePickerDialog?
  • ruby/rails: extending or including other modules
  • Get or convert Week of year to ISO week
  • Excel Date field value differs from c# dateTime by 1 day while reading excel file with EEPlus
  • Action Pack components in Rails
  • DateTime.Today for a specific time zone
  • accepts_nested_attributes_for practical form use for in Rails 3
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • htaccess add www if not subdomain, if subdomain remove www
  • Word Open XML Mail Merge
  • JSON response opens as a file, but I can't access it with JavaScript
  • Replace value with Factor in r data.table
  • Database structure design with variable amounts of fields
  • Accessing IRQ description array within a module and displaying action names
  • Submit form in a displaytag pagination
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Delete MySQLi record without showing the id in the URL
  • Unanticipated behavior
  • Comma separated Values
  • Trying to get generic when generic is not available
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal