# Curious why this doesn't work. (has\_many, belongs\_to)

**URL:** <https://www.ruby-forum.com/t/curious-why-this-doesnt-work-has-many-belongs-to/110664>\
**Category:** Rails\
**Created:** [July 29, 2007, 4:34am UTC](https://www.ruby-forum.com/t/curious-why-this-doesnt-work-has-many-belongs-to/110664 "2007-07-29T04:34:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [July 29, 2007, 4:34am UTC](https://www.ruby-forum.com/t/curious-why-this-doesnt-work-has-many-belongs-to/110664/1 "2007-07-29T04:34:54Z")

</div>

Two Models.

class Gm \< ActiveRecord::Base  
belongs\_to :pool

# mode code here.

end

class Pool \< ActiveRecord::Base  
has\_many :gms

# mode code here.

end

Testing code through the console.

> > gm\_list.each{ | gm | puts " GM name: #{ gm.user\_name } belongs to Pool: #{ gm.pool.pool\_name } " }; nil  
> > GM name: John belongs to Pool: RHP 07-08 Season - Career League  
> > GM name: Stef belongs to Pool: RHP 07-08 Season - Career League  
> > GM name: Chris belongs to Pool: RHP 07-08 Season - Career League  
> > =\> nil
> 
> >

Works nice!

Inversely… this doesn’t…

> > pool = Pool.find\_all  
> > =\> [#\<Pool:0xb7022dc8 @attributes={“last\_modified”=\>nil, “id”=\>“1”,  
> > “salary\_cap”=\>“40000000”, “pool\_name”=\>“RHP 07-08 Season - Career  
> > League”, “created”=\>nil}\>]
> 
> > puts pool.gm  
> > NoMethodError: undefined method `gm’ for #Array:0xb7022ee0  
> > from (irb):8
> 
> > puts pool.gms  
> > NoMethodError: undefined method `gms’ for #Array:0xb7022ee0  
> > from (irb):9
> 
> > puts pool.gms.each {|g| puts “Gm in this pool: #{gm.user\_name}” }; nil  
> > NoMethodError: undefined method `gms’ for #Array:0xb7022ee0  
> > from (irb):10

I thought that specifying a ‘belongs\_to’ and ‘has\_many’ relationship  
would automatically allow me to navigate my data in both directions.

Any comments are welcome.

Thanks!

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [July 29, 2007, 4:53am UTC](https://www.ruby-forum.com/t/curious-why-this-doesnt-work-has-many-belongs-to/110664/2 "2007-07-29T04:53:33Z")

</div>

Jean N. wrote:

> Two Models.
> 
> class Gm \< ActiveRecord::Base  
> belongs\_to :pool
> 
> # mode code here.
> 
> end
> 
> class Pool \< ActiveRecord::Base  
> has\_many :gms
> 
> # mode code here.
> 
> end
> 
> Testing code through the console.
> 
> > > gm\_list.each{ | gm | puts " GM name: #{ gm.user\_name } belongs to Pool: #{ gm.pool.pool\_name } " }; nil  
> > > GM name: John belongs to Pool: RHP 07-08 Season - Career League  
> > > GM name: Stef belongs to Pool: RHP 07-08 Season - Career League  
> > > GM name: Chris belongs to Pool: RHP 07-08 Season - Career League  
> > > =\> nil
> > 
> > >
> 
> Works nice!
> 
> Inversely… this doesn’t…
> 
> > > pool = Pool.find\_all  
> > > =\> [#\<Pool:0xb7022dc8 @attributes={“last\_modified”=\>nil, “id”=\>“1”,  
> > > “salary\_cap”=\>“40000000”, “pool\_name”=\>“RHP 07-08 Season - Career  
> > > League”, “created”=\>nil}\>]
> > 
> > > puts pool.gm  
> > > NoMethodError: undefined method `gm’ for #Array:0xb7022ee0  
> > > from (irb):8
> > 
> > > puts pool.gms  
> > > NoMethodError: undefined method `gms’ for #Array:0xb7022ee0  
> > > from (irb):9
> > 
> > > puts pool.gms.each {|g| puts “Gm in this pool: #{gm.user\_name}” }; nil  
> > > NoMethodError: undefined method `gms’ for #Array:0xb7022ee0  
> > > from (irb):10
> 
> I thought that specifying a ‘belongs\_to’ and ‘has\_many’ relationship  
> would automatically allow me to navigate my data in both directions.
> 
> Any comments are welcome.
> 
> Thanks!

Well I figured out why…

I forgot to realize that ‘pool’ (in my above examples) is not an  
instance of a pool in this case, it’s an array that needs to be iterated  
and then each iteration needs to have the ‘p.gms.each’ performed on  
it…

Like so:

> > pools = Pool.find\_all.each{ |p| p.gms.each { |g| puts “GMs in pool #{ g.user\_name }” } }; nil  
> > GMs in pool John  
> > GMs in pool Stef  
> > GMs in pool Chris  
> > =\> nil

Sorry about the spam. 🙂

(P.S. Damn I love code blocks in Ruby)
