Check if record exists in Model Rails -
i wondering if need create custom validation following scenario.
i have prediction model, in user submits predicted scores set of football matches, grouped fixture_date.
if user has submitted predictions these games show error message stating unable submit error exists, or maybe not show form if predictions dates exist.at moment can create multiple sets of predictions same games. validation better. how go stating if prediction exists date current_user not submit?
so setup looks 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 |prediction| prediction.new(prediction).save! end redirect_to root_path, :notice => 'predictions submitted successfully' rescue render 'new' end end end
im not sure relation between predictions , games. have game model? if should work:
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 1 prediction per game allowed, each user" } end
Comments
Post a Comment