Skip to content

Commit

Permalink
utilize reddit rank
Browse files Browse the repository at this point in the history
  • Loading branch information
Blair Anderson committed Apr 5, 2015
1 parent 5520445 commit 3fd5afb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ItemsController < ApplicationController
before_action :set_user_item, only: [:edit, :update, :toggle]

def index
order = params[:newest] ? {created_at: :desc} : {score: :desc}
order = params[:newest] ? {created_at: :desc} : {rank: :desc}

@items = Item.order(order).includes(:user)
@votes = @items.includes(:votes).each_with_object({}) do |item, object|
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/user_item_votes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class UserItemVotesController < ApplicationController
before_action :set_item
before_action :set_item, :require_login

def create
if current_user.votes.build(vote_params).save
Expand Down
15 changes: 13 additions & 2 deletions app/models/vote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,20 @@ def get_object
def update_votes
attrs = self.attributes.with_indifferent_access.slice(:votable_id, :votable_type)
count = Vote.where(attrs).count
get_object.update(
item = get_object

# Hot Score is a function defined inside:
# db/migrate/20150405200823_add_hot_score_function.rb

rank = item.class.where(id: item.id)
.select("id, hot_score(#{count}, 0, created_at) as hot_score")
.first.hot_score.to_i

item.update(
downvotes_count: 0,
upvotes_count: count,
score: count
score: count,
rank: rank
)
end
end
18 changes: 18 additions & 0 deletions db/migrate/20150405200823_add_hot_score_function.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class AddHotScoreFunction < ActiveRecord::Migration
# This is some sql borrowed from reddit:
# https://github.com/reddit/reddit/blob/master/sql/functions.sql
def up
execute <<-SQL
create or replace function
hot_score(ups integer, downs integer, date timestamp with time zone)
returns numeric as $$
select round(cast(log(greatest(abs($1 - $2), 1)) * sign($1 - $2) +
(date_part('epoch', $3) - 1134028003) / 45000.0 as numeric), 7)
$$ language sql immutable;
SQL
end

def down
execute "DROP FUNCTION IF EXISTS hot_score(integer, integer, timestamp);"
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150216170958) do
ActiveRecord::Schema.define(version: 20150405200823) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down

9 comments on commit 3fd5afb

@leonardykris
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello and thank you for open sourcing the code base.
I'm having a problem where once the vote is cast, ActiveRecord throws the following error:

ActiveRecord::StatementInvalid in UserItemVotesController#create
PG::UndefinedFunction: ERROR: function hot_score(integer, integer, timestamp with time zone) does not exist LINE 1: SELECT id, hot_score(1, 0, created_at) as hot_score FROM "i... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. : SELECT id, hot_score(1, 0, created_at) as hot_score FROM "items" WHERE "items"."id" = $1 ORDER BY "items"."id" ASC LIMIT 1

What I have tried:
Changed the format of the created_at to accept timestamp without time zone and then run bundle exec rake:db migrate to no avail.

Thank you.

@blairanderson
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leonardykris can you run the migration?

@blairanderson
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be easier if you start fresh and run these

$ git clone [email protected]:blairanderson/rails-hackernews-reddit-producthunt-clone.git
$ cd rails-hackernews-reddit-producthunt-clone
$ bundle
$ bundle exec rake db:create
$ bundle exec rake db:migrate
$ bundle exec rake db:seed

@leonardykris
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @blairanderson, yeah I can run the migration. I followed the instructions you listed above and cloned the repo to a new folder, but it still gives me the same error. Screenshot is attached below.
screen shot 2015-08-30 at 3 17 14 am

@blairanderson
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is really weird because i just did the exact same thing and its working fine...

The migration creates the function. That error says the function doesn't exist.

@blairanderson
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leonardykris can you run bundle exec rake db:migrate:up VERSION=20150405200823 and restart your server?

@blairanderson
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leonardykris

if you make an account on https://www.nitrous.io/ and try it there. I would love to help figure out why its not working for you.

@leonardykris
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried running the migration to that version and it still gives me the error. However, I tried deleting the database this time (in the previous test I didn't clear the database so I guess it wasn't a really 'fresh start') and running through the procedures again. I noticed that during migration, the function is added now and if I do recall it correctly, I didn't see those lines during my earlier migrations - which is really weird.

screen shot 2015-08-30 at 5 54 46 am

@leonardykris
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, the issue is solved now! Thanks for the lightning fast response and help!

Please sign in to comment.