次のシナリオのカスタム検証を作成する必要があるかどうか疑問に思っていました。
私は予測モデルを持っています。このモデルでは、ユーザーが一連のサッカーの試合の予測スコアを提出し、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