Weird Problem - probably a noob mistake

I was up late last night trying to find the cause of this, but I’m
stumped.

I have this relatively simple controller to load up a bio. It’s basicly
id, name, desc, type (I use STI).

class BioController < ApplicationController
layout ‘layouts/singlepanel’

def index
    @bio = Bio.find(:all, :conditions => ['site_id = ?', 

@session[‘site’].id])
render ‘bio/index’
end

end

However when I attempt to display the data in the view using this:

<%[email protected]%>

I get an error:
undefined method `name’ for #Array:0x3892618

Here is the strange part:

  1. I saved @bio to the session and looked at the contents and it all
    appears to be ok.
  2. If I change @bio = Bio.find(:all, :conditions => [‘site_id = ?’,
    @session[‘site’].id]) to @bio = Bio.find(1) it works fine.
  3. I ran the SQL using @bio = Bio.find(:all, :conditions => [‘site_id =
    ?’, @session[‘site’].id]) and it works fine.
  4. When I display

    <%[email protected]%>

    it displays “Array” as
    opposed to “Bio”.

I’m sure I’m doing something wrong, but I can’t see it at all. Any
ideas?

Joe C. wrote:

class BioController < ApplicationController
layout ‘layouts/singlepanel’

def index
    @bio = Bio.find(:all, :conditions => ['site_id = ?', 

@session[‘site’].id])
render ‘bio/index’
end

end

However when I attempt to display the data in the view using this:

<%[email protected]%>

I’m sure I’m doing something wrong, but I can’t see it at all. Any
ideas?

What has been returned from the find :all is more than one object. You
can either use:

@bio.first.name
or
@bio.each do |b|
… (e.g. b.name)
end

Joe C. wrote:

class BioController < ApplicationController
layout ‘layouts/singlepanel’

def index
    @bio = Bio.find(:all, :conditions => ['site_id = ?', 

@session[‘site’].id])
render ‘bio/index’
end

end

However when I attempt to display the data in the view using this:

<%[email protected]%>

I get an error:
undefined method `name’ for #Array:0x3892618

A few things :slight_smile:

  1. @bio should actually be @bios as find(:all) returns an array. You’ll
    need to iterate the array. If you’re fairly sure there’s only one with
    that site_id, you can use find(:first… )instead (it’ll return a single
    @bio).

  2. You don’t need the render call in the index method as it’ll happen
    automagically.

Alan

David wrote:

What has been returned from the find :all is more than one object. You
can either use:

@bio.first.name
or
@bio.each do |b|
… (e.g. b.name)
end

Arrggh, Thank you! I changed my find to:
@bio = Bio.find(:first, :conditions => [‘site_id = ?’,
@session[‘site’].id])

There can be only one of them, so I should have used ‘:first’.

On 19 Apr 2006, at 15:26, Joe C. wrote:

However when I attempt to display the data in the view using this:

<%[email protected]%>

I get an error:
undefined method `name’ for #Array:0x3892618

find(:all) will return an Array of matching objects, even if there’s
only one that matches.

If you need just one object, replace :all with :first, and you’ll get
a single object back.

Scott