Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3598086
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:11:56+00:00 2026-05-18T20:11:56+00:00

I have a Game model which has_many Rounds which has_many Shots. Per game, each

  • 0

I have a Game model which has_many Rounds which has_many Shots.
Per game, each cup hit with a shot should be unique. This is easy enough to do with validates_uniqueness_of :cup using a scope of :game_id.

However, how do I validate that each Shot is an increment of +1 of the last shot? I cannot have users select their first shot as having made cup 4. This would make no sense.

My form is using form_for @round which accepts nested attributes for exactly 6 shots.

How do I implement this validation? Do I need to refactor my view or completely rethink this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:11:56+00:00Added an answer on May 18, 2026 at 8:11 pm

    Since you are using Rails 3, you get some nice options here. I’m not sure that I understand your problem completely, but I’m assuming that you want some type of validation where the score starts at 1 and increments each time.

    Here’s a test.

      require 'test_helper'
    
        class ShotTest < ActiveSupport::TestCase
          test "score validations by game" do
            Shot.delete_all # Just to be sure. In a real test setup I would have handled this elsewhere.
    
            shot = Shot.new(:game_id => 1, :score => 1)
            assert shot.valid?
            shot.save!
    
            assert ! Shot.new(:game_id => 1, :score => 1).valid?
            assert ! Shot.new(:game_id => 1, :score => 3).valid?
            assert   Shot.new(:game_id => 1, :score => 2).valid?
    
            assert   Shot.new(:game_id => 2, :score => 1).valid?
          end
        end
    

    And an example model.

    # Stick this in a lib file somewhere
    class IncrementValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        record.errors[attribute] << "must increment score by +1 " unless value == (Shot.maximum(:score, :conditions => {:game_id => record.game_id} ).to_i + 1)
      end
    end
    
    class Shot < ActiveRecord::Base
      validates :score, :uniqueness => {:scope => :game_id}, :increment => true
    end
    

    Test output:

    $ ruby -I./test test/unit/shot_test.rb 
    Loaded suite test/unit/shot_test
    Started
    .
    Finished in 0.042116 seconds.
    
    1 tests, 5 assertions, 0 failures, 0 errors
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a game in which you can score from -40 to +40 on
I have a table with game scores, allowing multiple rows per account id: scores
I have my own game engine using C++ and OpenGL, but I have models
I have an game application I have written for Windows Mobile and I want
I have a little game written in C#. It uses a database as back-end.
I have written a game that uses GLUT, OpenGL and FMOD. The problem is
I have little knowledge of Flash but for a little Flash game I have
Ok guys just a small game: I have some specifications for a project. At
I have an OpenGL ES game that I am hacking together. One part of
I have develop an XNA game on computer 1. When I send it to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.