2

I'm trying to show all the posts that I like by current user.

I'm using Ruby on Rails, and the gems Devise and "Acts As Votable". I've been following the Acts As Votable guide but can't make it work.

I think this is the way to go:

@post.liked_by @user1
@post.downvote_from @user2

I created a new controller called dashboard:

class DashboardController < ApplicationController
  def index
        @post = Post.all.liked_by current_user
  end
end

But when I run my web I get:

undefined method `liked_by' for #<Post::ActiveRecord_Relation:0x9aa89b0>

What can I do?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Ezequiel Ramiro
  • 760
  • 1
  • 12
  • 29

2 Answers2

2

try @posts = current_user.find_up_voted_items in your controller and putting acts_as_voter in your User model.

Yorkshireman
  • 2,194
  • 1
  • 21
  • 36
  • @EzequielRamiro if this one works, surely it's better than the other answer? You want to show all of the posts that were liked by `current_user` - this achieves that. The 'best answer' you chose describes how to 'like' posts. As far as I can tell, that is a completely different thing to what you asked for in the original question. – Yorkshireman May 28 '16 at 11:11
2

The issue is that you're calling it on an ActiveRecord Relation, not on the model object, itself. A couple of minor changes, and you'll be all set.

First, we make the instance variable plural to show that it's receiving multiple records (a user can like multiple posts):

class DashboardController < ApplicationController
  def index
    @posts = Post.all.liked_by current_user
  end
end

Then, to process these, you'll want to navigate to the individual record or records. If you want to process the whole list, you can do this:

@posts.each do |post|
  post.liked_by @user1
  post.downvote_from @user2
end

This will apply the like and downvote to all of the posts. However, you can update only the first post, like this:

post = @posts.first
post.liked_by @user1
post.downvote_from @user2

If you already knew what post you wanted to vote on (maybe based on which was chosen in the UI), you could do this:

@post = Post.find_by(params[:id])
@post.liked_by @user1
@post.downvote_from @user2

Making sure to keep your plural and singular names distinct will help you keep in tune with Rails conventions and will help you easily identify when you're working with a collection or an individual object.

Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
  • does my answer not work? I think the gem already caters for this functionality (as per my answer), but I haven't tested this for myself. If I'm right, it would be a much better way to do it. – Yorkshireman May 26 '16 at 13:19
  • @Andy The OP was having difficulty querying. The error, as pointed out in the first sentence of my answer, is that he is attempting to use a model method (`liked_by` provided by `acts_as_votable`) on a collection, not the model object that he intended. This is like trying to use `String#gsub` on an array that contains strings - the method is being used on the wrong object. Your answer, while possibly correct for some *other* question, has no bearing on *this* question. You describe how to configure and retrieve voted items, and this is clearly not the problem that the OP is asking about. – Michael Gaskill May 26 '16 at 16:30
  • "I'm trying to show all the posts that I like by current user." This sounds like he's trying to retrieve all the posts from the database that have been 'liked' by `current_user`. I'm mystified as to why he's chosen your answer as the best one - makes even less sense since he's also said that my answer 'also works', but our answers do two completely different things. – Yorkshireman May 28 '16 at 11:55