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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:17:22+00:00 2026-05-27T05:17:22+00:00

I have a User model with devise, a Team model, a Player model and

  • 0

I have a User model with devise, a Team model, a Player model and a Trainer model. I want that when the User signs up, its associated team is created along with the associated trainer and players. I have set up the models as below:

user.rb

has_one :team
has_one :trainer, :through => :team
has_many :players, :through => :team
accepts_nested_attributes_for :team, :allow_destroy => true

before_save :create_team

def create_team
  @team = Team.new(params[:team])
  @team.user = self
  @team.save
end

team.rb

belongs_to :user
has_one :trainer
has_many :players
accepts_nested_attributes_for :trainer, :allow_destroy => true
accepts_nested_attributes_for :player, :allow_destroy => true

trainer.rb and player.rb

belongs_to :team

I have not added the create_trainer and create_player functions, since I want the user to select them later in the game. So they should be empty during the creation of the user.

But the sign up process gives the following error:

No association found for name `trainer'. Has it been defined yet?

and refers to the line:

accepts_nested_attributes_for :trainer, :allow_destroy => true

in team.rb. What is wrong with having the Trainer item not defined yet, if there is no validation of presence of Trainer defined in the Team model? I tried adding some lines to the Trainer model, to set the attributes to default values like:

morale.default => (5..12).to_a.sample

but it gave further errors, so is probably wrong. Any comment is greatly appreciated, especially anything criticising the basis of tinking here, since I am a noob.

  • 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-27T05:17:22+00:00Added an answer on May 27, 2026 at 5:17 am

    A few things:

    Dont use instance variables in your model. To access the team in the user model, just do team or self.team.

    Don’t use before_save since you don’t want to create the team each time you save your user.

    Your create_team method should be:

    after_create :my_create_team
    
    def my_create_team
        create_team #create an empty team
    end
    

    But if the data for the new team is already present in the form when the user signs up, then the team should automatically be created since you have accepts_nested_attributes_for :team.


    I’m going to answer some of your questions in the comments here:

    http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_one

    So when you add has_one :team you have now access to all these methods (build_team, create_team, team= etc)

    By “empty”, I just meant that if you just call create_team without any attributes, it would create a “default” team: no name etc. But it would be linked to your user though.

    If you wanted to just create an “empty” team, you could just do this I think:

    after_create :create_team
    

    Creating your own method would just allow you to pass default parameters.

    But you have probably added validation to the team, like validating the presence of its name.
    Anyways, since you have accepts_nested_attributes_for :team, :allow_destroy => true, it should create the team automatically if you have the required fields for the user’s team in the sign up form.

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

Sidebar

Related Questions

I have already created a User Model using devise, but I now want to
I have a Model User from Devise with that relations: user.rb # Relationships references_many
I have a User model that Devise manages - i.e. all users have email
I have a user model that is generated using Devise. I am extending this
I have a user model on which I check to make sure that the
I have a user model that requires the user to change their password every
So I have a User model that :has_many other models like Documents, Videos, Posts
I have a typical User model (username, password, name, etc). I want to allow
I have a user model that has a few different types, i.e. there is
I have one Devise model: User. I am needing two different login views, /login

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.