I was wondering if i would need to create a custom validation for the following scenario.
I have a Prediction model, in which a user submits their predicted scores for a set of football matches, they are grouped by fixture_date.
If a user has already submitted predictions for these games already i would like to show an error message stating they are unable to submit as the error exists, or maybe not show the form if the predictions for the dates exist.At the moment I can create multiple sets of predictions for the same games. Probably the validation would be better. How would i go about stating that if a prediction exists for that date for the current_user then do not submit?
So my setup looks like this so far
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 Predictions Controller
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