I have created a hacker news clone and I am trying to implement a voting system that ranks from most voted to least voted, based on the past 7 days. I know there are several gems to do this but I would like to do it once manually before using a gem.
First issue I am having, is with multiple votes. I want to have each user able to vote on as many links as they want but with a limit of one vote per link. I tried to make this happen in my votes_controller
but it is only allowing the links themselves to have one vote total instead of the user having one vote per link. Here is my method in votes_controller
:
def create
@vote = Vote.new(voter_params)
if @vote.save
redirect_to links_path, notice: "Thanks for voting!"
else
redirect_to links_path, notice: "Sorry, you can only vote on a link once."
end
end
My schema.rb
consists of users
, links
, and votes
with a user_id
attribute in both links
and votes
as listed here :
create_table "links", force: true do |t|
t.string "description"
t.string "url"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.string "user_name"
t.string "password_digest"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "votes", force: true do |t|
t.integer "user_id"
t.integer "link_id"
t.datetime "created_at"
t.datetime "updated_at"
end
How can I refactor my current action to allow votes for any link but only one vote per user?
Ok, my second and last question is, once I have that voting issue squared away, how can I sort them from most voted, to least voted, based on the past 7 days, going through the postgres server approach of course. It just seems to me that going through the db is more efficient. I'd appreciate any insight I can get, I am totally stumped, thanks!!
Here is my link model to give a better idea :
class Link < ActiveRecord::Base
validates :url, :presence => true, :url => true
belongs_to :user
has_many :votes
has_many :comments, :as => :commentable
def self.sort_by_votes
sorted_votes = Link.all.sort_by { |link| link.votes.count }
sorted_votes.reverse
end
end
and here is my view :
<% if current_user %>
<p style="float:right; padding-right: 10px;"> Logged in as: <%= current_user.name %> </p>
<% end %>
<div class="container" id="content">
<button class="btn btn-default btn-xs" style="float:right"><%= link_to "New Link", new_link_path %></button><br><br>
<table >
<thead>
<tr>
</tr>
</thead>
<tbody class="stories">
<% @links.sort_by_votes.each do |link| %>
<%= form_for link.votes.new do |f| %>
<%= f.hidden_field :link_id %>
<%= f.submit '▲' %>
<% end %>
<%= "#{link.votes.count} votes " %>
<%= link_to link.description, link.url %>
<small><%= "(#{link.url})" %></small>
<span class="date"><%= link.created_at.to_time.strftime("%b %-d, %Y @ %-I:%M%P") %></span>
<br>
<button class="btn btn-default btn-xs">
<%= link_to "Delete", link, :method => :delete, :data => { :confirm => "are you sure about that?" } %>
</button>
<button class="btn btn-default btn-xs">
<%= link_to "Comment", link_path(link)%>
</button><hr/>
<% end %>
</tbody>
</div>
You should use validations for this:
class Vote
#...
validates :user_id, uniqueness: { scope: :link_id }
Relationship counters are rather complex depending on your db and also generally produce complex time consuming queries. I'd use the counter_cache
option. There's a nice post about how to use those.
$ rails g migration add_votes_counter_to_link votes_count:integer
class AddVotesCounterToLinks < ActiveRecord::Migration
def change
add_column :links, :votes_count, :integer, default: 0
end
end
$ rake db:migrate
class Vote
belongs_to :link, counter_cache: true
end
class Link
has_many :votes
scope :most_voted, -> do
where(created_at: (Time.now - 7.days)..Time.now ) # created 7 days ago
.order('votes_count DESC') # ordered by votes_count
.limit(100) # first 100
end
Then you can do things like:
Link.most_voted