我有一个选民模型加入的用户和问题.用户可以对问题进行投票.他们可以向上或向下投票(在选民模型中记录).首先,我希望能够阻止用户在一个方向上投票.其次,我想允许用户投反对票.因此,如果他们投了票,他们仍然可以投票,这将取代上行投票.用户永远不能对问题进行两次投票.这是我的文件:
class Issue < ActiveRecord::Base has_many :associations,:dependent => :destroy has_many :users,:through => :associations has_many :Voterships,:dependent => :destroy has_many :users,:through => :Voterships belongs_to :app STATUS = ['Open','Closed'] validates :subject,:presence => true,:length => { :maximum => 50 } validates :description,:length => { :maximum => 200 } validates :type,:presence => true validates :status,:presence => true def cast_Vote_up!(user_id,direction) Voterships.create!(:issue_id => self.id,:user_id => user_id,:direction => direction) end end class Votership < ActiveRecord::Base belongs_to :user belongs_to :issue end class VotershipsController < ApplicationController def create session[:return_to] = request.referrer @issue = Issue.find(params[:Votership][:issue_id]) @issue.cast_Vote_up!(current_user.id,"up") redirect_to session[:return_to] end end class User < ActiveRecord::Base authenticates_with_sorcery! attr_accessible :email,:password,:password_confirmation validates_confirmation_of :password validates_presence_of :password,:on => :create validates_presence_of :email validates_uniqueness_of :email has_many :associations,:dependent => :destroy has_many :issues,:through => :Voterships end
解决方法
原文地址:https://www.jb51.cc/ruby/270671.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。