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 } " }; nilGM 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.gmNoMethodError: undefined method `gm'' for #<Array:0xb7022ee0> from (irb):8>> puts pool.gmsNoMethodError: undefined method `gms'' for #<Array:0xb7022ee0> from (irb):9>> puts pool.gms.each {|g| puts "Gm in this pool: #{gm.user_name}" }; nilNoMethodError: 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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jean Nibee
2007-Jul-29 02:53 UTC
Re: Curious why this doesn''t work. (has_many, belongs_to)
Jean Nibee 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 }" } }; nilGMs 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) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---