Closed cwcox closed 7 years ago
I think you're super close. Try:
def add_teams
12.times do |i|
self.teams.create!(name: "Team #{i}")
end
end
That does seem to be more correct, so I am guessing there is more than one issue; but, I am still getting the same method undefined error. Do I need to add a reference to the method file in the controller file? Something like require 'league.rb'? Do I need to add an attr to the method class so that the method is accessible?
Shouldn't need to require anything as the active record models are classes and will be loaded into the environment by rails so they are accessible from the controller. Methods in ruby (which teams
and add_teams
both are) are public unless otherwise specified so that shouldn't be the problem either. Is your latest code pushed? Going to mess around with it for a minute and get back to you
Ah, you've got self.team.create!(name: "Team #{i}")
in this line. You want and s
at the end of teams
(it's correct in the code snippet you added). After that works fine for me.
Strange, I made the change and am still getting the same error.
My current league model is:
class League < ApplicationRecord
has_many :teams, :dependent => :destroy
accepts_nested_attributes_for :teams
def add_teams
12.times do |i|
self.teams.create!(name: "Team #{i}")
end
end
end
Could there be other difference in your set up vs mine that could cause this issue?
There shouldn't be. I downloaded, bundle installed, created the database, migrated, seeded, added the s and then from the console could run League.create.add_teams
. Could you copy the error and stack trace you're seeing?
app/controllers/leagues_controller.rb:29:in `create'
Started POST "/leagues" for 127.0.0.1 at 2017-08-08 12:33:38 -0400
Processing by LeaguesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"n9Hbs/NGQS+s0oKI/uTCu243LA25ARCWp1wv2tzbW8P8NjSxZEIDaIKa18vJd0rMNfch9JX9bTnCqC+SYWNjew==", "league"=>{"name"=>"League 5114"}, "commit"=>"Create League"}
(0.1ms) BEGIN
SQL (0.4ms) INSERT INTO "leagues" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2017-08-08 16:33:38.263077"], ["updated_at", "2017-08-08 16:33:38.263077"]]
(0.5ms) COMMIT
Completed 500 Internal Server Error in 8ms (ActiveRecord: 1.0ms)
NoMethodError (undefined method `add_teams' for #<League:0x007fe183fc4218>):
I've tried calling it in the League controller's create method multiple ways and have the same error each time.
That is strange. Do you get the same thing in the console?
Yeah same thing.
Are there in person office hours this afternoon?
yeah, juan will be on. Make sure everything you have is pushed and I'll try again
Great. I just pushed to the repo, thanks.
Actually, check that. I am not getting an error in the console, only in the browser, so it must be an issue in the controller.
Juan was able to help. Had to restart the server...embarrassingly simple
I have a class League that has many Teams. When a new league is created 12 instances of Team should be created as well (both league and team only have a 'name' column in the database). the relationship between Leagues and Teams is one to many and they have the following routing:
the model method is:
The controller method is:
The error I get after submitting the 'Create League' form is that "add_teams" is an undefined method. I also suspect this is not the correct approach to this problem from the start.
Repo: https://github.com/cwcox/fantasy_draft_simulator