Skip to content
This repository has been archived by the owner on Nov 13, 2021. It is now read-only.

Find_with_reputation in Rails 4 #62

Open
bgribbin opened this issue Jul 22, 2013 · 7 comments
Open

Find_with_reputation in Rails 4 #62

bgribbin opened this issue Jul 22, 2013 · 7 comments

Comments

@bgribbin
Copy link

Hi,

Does this method work with Rails 4 yet?

This is my gem file:
gem 'activerecord-reputation-system', github: 'NARKOZ/activerecord-reputation-system', branch: 'rails4'

When I try this in my controller nothing gets ordered:

find_with_reputation(:votes, :all, :order => "votes desc")

Thanks

@ghost
Copy link

ghost commented Oct 7, 2013

same here.

@mpgarate
Copy link

When trying to use this method, my specs print the following error:

DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g. Post.where(published: true).load). If you want to get an array of records from a relation, you can call #to_a (e.g. Post.where(published: true).to_a). (called from find_with_reputation at /home/vagrant/.rvm/gems/ruby-2.0.0-p247/bundler/gems/activerecord-reputation-system-01197ad78cac/lib/reputation_system/finder_methods.rb:30)

I get this when calling something like:
@posts = Post.find_with_reputation(:votes, :all, order: 'votes desc')

@rjswenson
Copy link

I found a work around, I have images which I am trying to sort desc based upon upvotes:

My Solution:

In images_controller.rb (index method)

image_ids = ActiveRecord::Base.connection.execute("SELECT target_id FROM rs_reputations WHERE target_type = 'Image' ORDER BY value DESC")
image_ids = image_ids.map { |item| item = item[0] }
@images = []
image_ids.each { |id| @images << Image.find(id) }

I was looking at my server logs and it looks like the instead of pulling all images with one query, descending, 'find_with_reputation' queries for each item (and therefore cannot sort). It also doesnt query 'value'.

  ReputationSystem::Reputation Load (0.2ms)  SELECT "rs_reputations".* FROM "rs_reputations" WHERE "rs_reputations"."reputation_name" = 'votes' AND "rs_reputations"."target_id" = 14 AND "rs_reputations"."target_type" = 'Image' LIMIT 1
  ReputationSystem::Reputation Load (0.3ms)  SELECT "rs_reputations".* FROM "rs_reputations" WHERE "rs_reputations"."reputation_name" = 'votes' AND "rs_reputations"."target_id" = 15 AND "rs_reputations"."target_type" = 'Image' LIMIT 1
  ReputationSystem::Reputation Load (0.3ms)  SELECT "rs_reputations".* FROM "rs_reputations" WHERE "rs_reputations"."reputation_name" = 'votes' AND "rs_reputations"."target_id" = 17 AND "rs_reputations"."target_type" = 'Image' LIMIT 1

Solution:
Either condense the Query into one, or push each query result into an array and then sort by value.

@pjfamig
Copy link

pjfamig commented Mar 25, 2014

@rjswenson's solution worked for me.

Note that to paginate the results with will_paginate, after image_ids.each just call @images = @images.paginate(page: params[:page], :per_page => 10). And make sure to require 'will_paginate/array'.

@NARKOZ
Copy link
Contributor

NARKOZ commented Mar 29, 2014

See http://stackoverflow.com/q/22617224 for a possible fix. In short, use:

reorder('votes desc').find_with_reputation(:votes, :all)

@caiosba
Copy link
Contributor

caiosba commented Sep 8, 2014

FYI, I have a fork with a Rails 4 branch... all tests are passing and I'm using it on a Rails4 + Ruby 2 application without problems... you can try that and let me know if things are not working there: https://github.com/caiosba/activerecord-reputation-system/tree/rails4

@tengcong
Copy link

tengcong commented Sep 6, 2015

try with

Post.with_reputation(:votes).order("votes DESC")

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants