Forum: Ruby on Rails Curious why this doesn't work. (has_many, belongs_to)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jean N. (Guest)
on 2007-07-29 06:34
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!
Jean N. (Guest)
on 2007-07-29 06:53
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)
This topic is locked and can not be replied to.