
Untitled
By: a guest on
May 30th, 2012 | syntax:
None | size: 0.51 KB | hits: 9 | expires: Never
# app/models/vote.rb
class Vote < ActiveRecord::Base
belongs_to :post
validates_presence_of :user_ip, :post
end
class Post < ActiveRecord::Base
has_many :votes
def voted_from?(ip)
!self.votes.where(:user_ip => @ip).empty?
end
end
# app/controllers/votes_controller.rb
class VotesController < ApplicationController
def create
ip = request.remote_ip
post = Post.find(params[:id])
unless post.voted_from?(params[@ip])
Vote.create(params[:vote])
end
end
end