twitter / activerecord-reputation-system

An Active Record Reputation System for Rails
Apache License 2.0
1.33k stars 122 forks source link

Find_with_reputation in Rails 4 #62

Open bgribbin opened 11 years ago

bgribbin commented 11 years ago

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 commented 11 years ago

same here.

mpgarate commented 11 years ago

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 commented 10 years ago

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 commented 10 years ago

@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 commented 10 years ago

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

reorder('votes desc').find_with_reputation(:votes, :all)
caiosba commented 10 years ago

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 commented 9 years ago

try with

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