i''ve got 3 tables: here are the models class Category < ActiveRecord::Base #category.rb has_many :employees end class Employee < ActiveRecord::Base #employee.rb belongs_to :category has_one :bio end class Bio < ActiveRecord::Base #bio.rb belongs_to :employee end i''m trying to get a recordset of all the bios in a view using: @category.employees.bio each do |bio| ... but i am getting "undefined method `bio'' for Employee:Class" @category.employees does return a valid recordset Any ideas? 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 -~----------~----~----~----~------~----~------~--~---
I think you want: @category.employees.each { |emp| emp.bio } On 11/1/06, Ryan <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > i''ve got 3 tables: > > here are the models > > class Category < ActiveRecord::Base #category.rb > has_many :employees > end > > > class Employee < ActiveRecord::Base #employee.rb > belongs_to :category > has_one :bio > end > > > class Bio < ActiveRecord::Base #bio.rb > belongs_to :employee > end > > > i''m trying to get a recordset of all the bios in a view using: > > @category.employees.bio each do |bio| > > ... but i am getting "undefined method `bio'' for Employee:Class" > > > > @category.employees does return a valid recordset > > > > Any ideas? > > > Thanks! > > -- > Posted via http://www.ruby-forum.com/. > > > >-- Thanks, -Steve http://www.stevelongdo.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 -~----------~----~----~----~------~----~------~--~---
Ryan wrote: d> > > i''m trying to get a recordset of all the bios in a view using: > > @category.employees.bio each do |bio| > > ... but i am getting "undefined method `bio'' for Employee:Class" > > > > @category.employees does return a valid recordset > > > > Any ideas? >in this case @category.employees is an array and doesn''t support the method bio(), try: @category.employees.each {|emp| puts emp.bio} -- 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 -~----------~----~----~----~------~----~------~--~---
Ilan, No luck there. It seems that the method "bio" is not being recognized in the Employee Class. Period. To test this, I tried @employees.bio.each do |bio|, and it also gives me the same message. But again, @employees does return a valid recordset -- 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 -~----------~----~----~----~------~----~------~--~---
Ryan wrote:> Ilan, > > No luck there. > > It seems that the method "bio" is not being recognized in the Employee > Class. Period. > > > To test this, I tried @employees.bio.each do |bio|, and it also gives me > the same message. >You misunderstood Ilan. bio is an instance method of an Employee instance, not a method of the @employees *collection*. So you call .bio on each *employee* object individually, not on the collection as a whole. To collect up all the bios for each employee, you can do something like this in your controller: @bios = @category.employees.collect { |employee| employee.bio } Now @bios is an array of all of the bio objects for the given category. Presumably in your view you could now iterate over this collection: <% @bios.each do |bio| <p><%= bio.name %> %> Make sense? -- 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 -~----------~----~----~----~------~----~------~--~---
Jeff, Thanks! That does make sense, and I''ve been able to proceed a bit. Although, in your example above: @bios = @category.employees.collect { |employee| employee.bio } returns Employee records that do not have bios, evidenced by some bio records returning nil. I thought I''d be able to call something like: @category.employees.bio and get the appropriate information. If only certain employees have bios, and employees belongs to categories ... I wanted to see only certain bios belonging employees who belong to 1 category. Am I approaching this incorrectly? -- 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 -~----------~----~----~----~------~----~------~--~---
Ryan wrote:> Jeff, > > Thanks! That does make sense, and I''ve been able to proceed a bit. > Although, in your example above: @bios = @category.employees.collect { > |employee| employee.bio } returns Employee records that do not have > bios, evidenced by some bio records returning nil. >You can filter those out by calling .compact on the collectoin: @bios = @category.employees.collect { |employee| employee.bio }.compact I don''t think there''s a more direct way to do what you want, except maybe to write your own SQL to do the query (which would be faster in this case). Feels like there should be a better way though, right? If I can think of a better way I''ll let you know. Jeff www.softiesonrails.com -- 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 -~----------~----~----~----~------~----~------~--~---
Perfect! Thanks so much Jeff, .compact removed the nil element as described. array.compact http://corelib.rubyonrails.com/classes/Array.html#M000454 I appreciate the help! And yes, it seems like there should be a better way to do this. Hmm... how? -- 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 -~----------~----~----~----~------~----~------~--~---
bios = Bio.find(:all) On 11/1/06, Ryan <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Perfect! > > Thanks so much Jeff, .compact removed the nil element as described. > > array.compact > http://corelib.rubyonrails.com/classes/Array.html#M000454 > > > I appreciate the help! > > > And yes, it seems like there should be a better way to do this. > > Hmm... how? > > > > > -- > Posted via http://www.ruby-forum.com/. > > > >-- Thanks, -Steve http://www.stevelongdo.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 -~----------~----~----~----~------~----~------~--~---