所以你有某种Ballot
模型。Ballot
有很多Submission
s,提交有很多Vote
s。还有一个WinningSubmission
模型。Ballot
有一个WinningSubmission
。
我认为Ballot
包含 X 和 Y 的时间戳可能是最简单的。
如果您不希望有一些 cron 作业来轮询已完成投票,您可以运行一个通用before_filter
输入ApplicationController
(或者,最好为这个过滤器创建一个空白控制器,并让其他管理投票的控制器继承从中)检查每个请求以查看是否有任何选票已关闭,但也没有获胜者;然后在继续请求之前计算获胜者。它就像一个工作人员,只是它在网络服务器中,所以它增加了一些响应延迟。
至于提交和投票的控制器/视图架构create
,我可能会有三次嵌套控制器:
resources :ballots do # These blocks may need to pass in their object
# depending on your rails version.
resources :submissions do
# POST to ballot_submissions_path(@ballot) creates subs's.
resources :votes # POST to ballot_submission_votes_path(@b, @s) creates votes.
end
end
我必须更多地了解您的交互设计,以帮助“创建”操作之外的任何视图/ajax 级别交互,但我猜你要么在投票提交时采用单页索引样式设计级别,或者使用一系列视图,每个状态对应一个选票。#index
为了简单起见,这些可能会散布在各个控制器的操作中。
如上所述,如果您不想使用 cronjob 或工作人员,我将拥有BallotsController
, SubmissionsController
,并且VotesController
都< BallotCompleterController
像这样:
class VotesController < BallotCompleterController
#your vote handling actions would go here.
end
class BallotCompleterController < ApplicationController
before_filter :complete_unfinished_ballots
protected
def complete_unfinished_ballots
Ballot.expectant.calculate_all!
end
#and that's all that's in here
end
class Ballot < ActiveRecord::Base
#...has_many etc's
named_scope :expectant, lambda{
{:select => "ballots.*",
:conditions => ['votes_until < ? and winning_submissions.id is null', Time.current],
:joins => 'left outer join winning_submissions
on winning_submissions.ballot_id = ballots.id',
:readonly => false} }
def self.calculate_all!
self.each(&:'calculate_winning_submission!')
end
def calculate_winning_submission!
#calc and save the winning_submission for this ballot
end
end