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

  • SEARCH
  • Home
  • 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 4107136
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:27:58+00:00 2026-05-20T21:27:58+00:00

(I apologize for being completely rails illiterate here, I hope I have given enough

  • 0

(I apologize for being completely rails illiterate here, I hope I have given enough info)

I am building players that are associated with games, and I am wondering how I get a validation to work for a game when I am building a player. So I have:

class Game < ActiveRecord::Base
  has_many :players, :dependent => :destroy

  #does not work or is ineffective due to how I made my player's create in the controller
  validates :players, :length => { :maximum => 6 }
end


class Player < ActiveRecord::Base
  belongs_to :game
end

There is also an association to user (player belongs to both a game and a user) but thats irrelevant for now.

In my players controller I have:

  def create
    @game = Game.find(params[:game_id])

    @players_in_game = Array.new
    @game.terra_players.each do |i|
      @players_in_game.push(i.user_id)
    end

      @player = current_user.terra_players.build(:terra_game => @terra_game)
      if @player.save
        redirect_to @game
      else
        render 'new'
      end
  end

Which successfully makes a new player and adds it to the game.

But the validation in class Game does not work, presumably because I am not calling create/update/update_attributes for my Game model.

How can I get the validation to run? Should I be remaking def create to use @game.create/update/update_attribute? If so, how?

Thanks for your help.

  • 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-20T21:27:59+00:00Added an answer on May 20, 2026 at 9:27 pm

    Not sure what exactly you are trying to accomplish, but here are some thoughts that hopefully lead you down a better path.

    1. You can not validated the maximum number of associated objects with default rails validations. You should be able to write a custom validation.

    2. Your logic using each and push seems very un-rubish and should probably be something like

      @players_in_game = @game.terra_players.map(&:user_id)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I apologize for being rather vague here, but I'm working on a project involving
I apologize for asking such a generalized question, but it's something that can prove
I apologize to move it from here as there was some confusion and thanks
This is my first post here, therefore apologize for any blunders. I'm developing a
I have spent quite some time going through similar questions here and have not
I apologize in advance for being too vague, if you need any precision I
I am going to apologize in advance for being extremely vague, but my knowledge
I apologize for the weird question wording... here's the design problem: I am developing
Programming is so new to me that I apologize for not knowing how to
I apologize for being a bit verbose in advance: if you want to skip

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.