ruby-on-railsrubyrubygemsruby-on-rails-5acts-as-votable

How do I get acts_as_votable results for a specific model?


I'm working with the acts_as_votable gem for a project that will allow users to 'like' their favorite courses and their favorite guides. The favorited guides will then show up on one page and the favorited courses on another. I'm having trouble with retrieving model specific results in my controller below is code that works but is not scoping to a specific controller.

class FavoritesController < ApplicationController
 def guides

 end

 def courses
  user = current_user
  @courses = user.find_up_voted_items
 end
end

This is the only code I've gotten to work, I realize there is nothing in the controller currently to narrow down the results to a specific model but I wasn't able to get anything I tried to work.


Solution

  • From the acts_as_votable docs:

    Members of an individual model that a user has voted for can also be displayed. The result is an ActiveRecord Relation.

    @user.get_voted Comment
    
    @user.get_up_voted Comment
    
    @user.get_down_voted Comment
    

    https://github.com/ryanto/acts_as_votable

    So in your case I would use:

    class FavoritesController < ApplicationController
      before_action :get_user, only: %i[guides courses]
    
      def guides
        @guides = @user.get_up_voted Guide
      end
    
      def courses
        @courses = @user.get_up_voted Course
      end
    
      private
    
      def get_user
        @user = current_user
      end
    end