Associations errors

undefined method `each' for 0:Fixnum at line @school = School.find_by_title(params[:school_id].gsub('-', " "))

  for team in @school.teams ........

end

@school does exist cause i tried with .find(:first) and it gave the same error.

Also I'm getting undefined method `reject' for #<Game:0x24ca1c4> From @home.game_ids = @game

    @game = Game.new(params[:game])     @game.opponent = params[:team][:name]

    @home = Team.find_by_user_id(current_user.id)     @opponent = Team.find_by_name(@game.opponent)

      if @game.save         flash[:notice] = 'Game was successfully created.'         @home.game_ids = @game         @opponent.game_ids = @game

Here are my models class Game < ActiveRecord::Base   has_and_belongs_to_many :teams end class Player < ActiveRecord::Base   belongs_to :team end class School < ActiveRecord::Base   has_many :teams   belongs_to :users

  def to_param     "#{title.gsub(/[^a-z0-9]+/i, '-')}"   end end class Team < ActiveRecord::Base   has_many :players   has_and_belongs_to_many :games   belongs_to :school   belongs_to :user

  def to_param     "#{name.gsub(/[^a-z0-9]+/i, '-')}"   end

end class User < ActiveRecord::Base   has_one :team   has_one :school end

edberner wrote:

undefined method `each' for 0:Fixnum at line @school = School.find_by_title(params[:school_id].gsub('-', " "))

  for team in @school.teams ........

end

Not sure about this one, but it looks like @school.teams is returning a 0, not an array of teams. "each" is called for you when you use a for/in loop. It is the equivalent of

  @school.teams.each { |team| ... }

So "undefined method `each' for 0:Fixnum" means "each" is being called on the integer 0.

Also I'm getting undefined method `reject' for #<Game:0x24ca1c4> From @home.game_ids = @game

        @home.game_ids = @game         @opponent.game_ids = @game

game_ids= is expecting an array of integers, your are passing it a model object. You probably just want to insert the game in the existing association. Try this:

  @home.games << @game   @opponent.games << @game

Thanks. It is starting to make sense to stop referring to elements by their rudimentary primary and secondary ids.

Why might it not be getting the array of teams. I clearly have it set up in the model?

It's interesting because now I get that same error here undefined method `each' for 1:Fixnum

  def update     @game = Game.find(params[:id])

    for team in @game.teams       team.increment! :games     end

Does anybody know why this is happening? Thanks.

Got it. Calling team.games returned the array of my association. Not the integer field I wanted. Changed field name and all is ok.