# Associations errors

**URL:** <https://www.ruby-forum.com/t/associations-errors/134108>\
**Category:** Rails\
**Created:** [April 7, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/associations-errors/134108 "2008-04-07T22:29:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 7, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/associations-errors/134108/1 "2008-04-07T22:29:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 7, 2008, 10:39pm UTC](https://www.ruby-forum.com/t/associations-errors/134108/2 "2008-04-07T22:39:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 7, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/associations-errors/134108/3 "2008-04-07T22:54:23Z")

</div>

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?

On Apr 7, 4:39 pm, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 7, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/associations-errors/134108/4 "2008-04-07T23:11:58Z")

</div>

Does anybody know why this is happening? Thanks.

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 8, 2008, 12:55am UTC](https://www.ruby-forum.com/t/associations-errors/134108/5 "2008-04-08T00:55:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 7, 2008, 11:00pm UTC](https://www.ruby-forum.com/t/associations-errors/134108/6 "2008-04-07T23:00:49Z")

</div>

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

```

On Apr 7, 4:39 pm, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
