ga-wdi-exercises / project2

[project]
1 stars 30 forks source link

Calling class method in controller #901

Closed cwcox closed 7 years ago

cwcox commented 7 years ago

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:

resources :leagues do
    resources :teams
  end

the model method is:

def add_teams
    12.times do |i|
      league.teams.create!(name: "Team #{i}")
    end
  end

The controller method is:

def create
    @league = League.new(league_params)
    @league.add_teams
    respond_to do |format|
      if @league.save
        format.html { redirect_to @league, notice: 'League was successfully created.' }
        format.json { render :show, status: :created, location: @league }
      else
        format.html { render :new }
        format.json { render json: @league.errors, status: :unprocessable_entity }
      end
    end
  end

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

jsm13 commented 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
cwcox commented 7 years ago

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?

jsm13 commented 7 years ago

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

jsm13 commented 7 years ago

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.

cwcox commented 7 years ago

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?

jsm13 commented 7 years ago

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?

cwcox commented 7 years ago
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.

jsm13 commented 7 years ago

That is strange. Do you get the same thing in the console?

cwcox commented 7 years ago

Yeah same thing.

Are there in person office hours this afternoon?

jsm13 commented 7 years ago

yeah, juan will be on. Make sure everything you have is pushed and I'll try again

cwcox commented 7 years ago

Great. I just pushed to the repo, thanks.

cwcox commented 7 years ago

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.

cwcox commented 7 years ago

Juan was able to help. Had to restart the server...embarrassingly simple