Undefined method & has_one

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!

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

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|

<%= bio.name %> %>

Make sense?

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}

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?

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

I think you want:

@category.employees.each { |emp| emp.bio }

On 11/1/06, Ryan [email protected] wrote:


Thanks,
-Steve
http://www.stevelongdo.com

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?

bios = Bio.find(:all)

On 11/1/06, Ryan [email protected] wrote:

I appreciate the help!
Posted via http://www.ruby-forum.com/.


Thanks,
-Steve
http://www.stevelongdo.com