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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:36:19+00:00 2026-06-02T06:36:19+00:00

So, here is the deal: I have two models, a Player and a Team.

  • 0

So, here is the deal: I have two models, a Player and a Team. Every team has_many players and every player belongs_to a team. The code (omitting a few unnecessary atributes from the player model):

class Team < ActiveRecord::Base
  attr_accessible :name, :points, :rank
  has_many :players

class Player < ActiveRecord::Base
  attr_accessible :name, :age, :pos, :team
  belongs_to :team

I am using seeds.rb to create a few teams and a bunch of players and then I try to put players into teams (that is, I try to actually implement the “has_many” and “belongs_to” associations). The code:

rndm = rand(1..10) #random number of teams

rndm.each do |i| #create rndm teams
  i = i.to_s
  Team.create(name: 'Team'+i, points: 0, ranking: 0)
end

for i in 1..rndm*22 do #create rndm*22 players (ideally 22 for each team)
  i = i.to_s
  Player.create(name: 'Player'+i, age: rand(15..35), 
                pos: 'Not specified', team: Team.find(rand(1..rndm)))
end

Now, the above code creates the teams and the players just fine. However, it does not associate players with teams. It does not associate them with a team at all. That is, when I type “@player = Player.find(1)” and “@player.team” in the rails console all I get is “=> nil”. BUT, when I specify the team myself (still in the console) by typing “@player.team = Team.find(1)”, the association is achieved just fine (resulting in the “player.team” argument to return the details for the team with team id: 1).

I assume I have to modify the Player controller or something like that? If so, what’s different when I associate the two models from the console rather than from the seeds.rb?

  • 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-02T06:36:21+00:00Added an answer on June 2, 2026 at 6:36 am

    In order for you’re seeding scheme to work, you’d have to be setting team_id: instead of :team when you create the Player.

    for i in 1..rndm*22 do #create rndm*22 players (ideally 22 for each team)
      i = i.to_s
      Player.create(name: 'Player'+i, age: rand(15..35), 
                pos: 'Not specified', team_id: Team.find(rand(1..rndm))).id
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the deal - I have two files, main.html and processing.php. Within the php
Ok. So here is the deal. I have two entities - Product and Parts.
Here's the deal : I have Publication objets in my application. I also have
Here's the deal. I have an XML document with a lot of records. Something
Here's the deal. I have a big class hierarchy and I have this one
Here's the deal. Is there a way to have strings tokenized in a line
Here is the deal, in my Java project I have to make a composite
I have to deal with text files in a motley selection of formats. Here's
The intent here is to deal with obfuscated passwords for resources. We have an
I have been struggling with this for months in my project. Here's the deal:

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.