0

次のシナリオのカスタム検証を作成する必要があるかどうか疑問に思っていました。

私は予測モデルを持っています。このモデルでは、ユーザーが一連のサッカーの試合の予測スコアを提出し、fixture_date でグループ化されています。

ユーザーがこれらのゲームの予測を既に送信している場合、エラーが存在するため送信できないことを示すエラー メッセージを表示するか、日付の予測が存在する場合はフォームを表示しない可能性があります。同じゲームの予測の複数のセット。おそらく検証はより良いでしょう。current_user のその日付の予測が存在する場合、提出しないとどのように述べればよいでしょうか?

だから私のセットアップはこれまでのところこのように見えます

class Prediction < ActiveRecord::Base
  attr_accessible :home_team, :away_team, :home_score, :away_score, :fixture_date,   :fixture_id, :user_id

has_one :fixture
end

class Fixture < ActiveRecord::Base
  attr_accessible :home_team, :away_team, :fixture_date, :kickoff_time, :prediction_id
end

予測コントローラー

 def index
   @predictions = current_user.predictions if current_user.predictions
 end

 def new
   @prediction = Prediction.new
 end

 def create
  begin
  params[:predictions].each do |prediction|
    Prediction.new(prediction).save!
  end
  redirect_to root_path, :notice => 'Predictions Submitted Successfully'
rescue
  render 'new'
 end
end
end
4

1 に答える 1

1

予測とゲームの関係についてはよくわかりません。Gameモデルはいますか?もしそうなら、このようなものがうまくいくはずです:

class Prediction < ActiveRecord::Base
  attr_accessible :home_team, :away_team, :home_score, :away_score, :fixture_date, :fixture_id, :user_id

  has_one :fixture

  validates :fixture_id, :uniqueness => { :scope => :user_id,
:message => "only one prediction per game is allowed, for each user" }
end
于 2013-05-10T10:14:05.547 に答える