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 7907589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:31:11+00:00 2026-06-03T11:31:11+00:00

In my RoR I have a table games , stats , and players .

  • 0

In my RoR I have a table games, stats, and players. Each game has many players, each player has many ‘stats,’ and each games has many stats through players. What i want to be able to do is in on my edit games form, i want there to be a a row of fields to add a new stat row, one per each players the game has. Ive been reading a lot about nested_attributes, but found new good resources how to fully do 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-06-03T11:31:14+00:00Added an answer on June 3, 2026 at 11:31 am

    UPDATE: Here’s an updated set of classes based on the new associations you’ve stated in your comment

    # models/game.rb
    class Game < ActiveRecord::Base
      has_many :teams
      accepts_nested_attributes_for :teams
      attr_accessible :name, :teams_attributes
    end
    

    # models/team.rb
    class Team < ActiveRecord::Base
      belongs_to :game
      has_many :players
      accepts_nested_attributes_for :players
      attr_accessible :name, :players_attributes
    end
    

    # models/player.rb
    class Player < ActiveRecord::Base
      belongs_to :team
      has_many :stats
      accepts_nested_attributes_for :stats, reject_if: proc { |attributes| attributes['name'].blank? }
      attr_accessible :name, :stats_attributes
    end
    

    # models/stat.rb
    class Stat < ActiveRecord::Base
      belongs_to :player
      attr_accessible :name
    end
    

    # controllers/games_controller.rb
    class GamesController < ApplicationController
      def edit
        @game = Game.find(params[:id])
        @game.teams.each do |team|
          team.players.each do |player|
            player.stats.build
          end
        end
      end
    
      def update
        @game = Game.find(params[:id])
        if @game.update_attributes(params[:game])
          render "show"
        else
          render text: "epic fail"
        end
      end
    end
    

    # games/edit.html.erb
    <%= form_for @game do |f| %>
      <%= f.fields_for :teams do |tf| %>
        <p>Team: <%= tf.object.name %></p>
        <%= tf.fields_for :players do |pf| %>
          <p>Player: <%= pf.object.name %></p>
          <%= pf.fields_for :stats do |sf| %>
            <%= sf.text_field :name %>
          <% end %>
        <% end %>
      <% end %>
      <%= f.submit %>
    <% end %>
    

    Note this doesn’t do any sort of ajax “add another stat” or anything fancy. It just sticks one extra blank field at the end for each player. If you needed more, you could build more blank stat objects in the GamesController#edit action or implement some fancy pants javascript. Hopefully that will get you close enough to be able to get your real data working.

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

Sidebar

Related Questions

I have a RoR project on my Windows 7 PC. I want to create
I have a RoR application running on box1...it obviously has Ruby, RubyGems, and Rails
Which is your preference? Let's say we have a generic Product table that has
I have quantity and quantity_received in a table. I want to populate quantity_received with
I have two models Users and Friendships in my RoR application The friendships table
I have a RoR application (ruby v1.8.7; rails v2.3.5) that is caching a page
I'm new with RoR and I have a controller ( UsersController ) where I
I have recently moved my RoR app on the Heroku platform, and almost everything
I have an almost new setup of ROR 3.1 here and have already generated
I have been working for several months on a RoR project with fellows university

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.