barerd
barerd

Reputation: 845

Creating many related models before_save

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.

Upvotes: 1

Views: 1721

Answers (1)

Robin
Robin

Reputation: 21884

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.

Upvotes: 1

Related Questions