# Weird Problem - probably a noob mistake

**URL:** <https://www.ruby-forum.com/t/weird-problem-probably-a-noob-mistake/58199>\
**Category:** Rails\
**Created:** [April 19, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/weird-problem-probably-a-noob-mistake/58199 "2006-04-19T16:26:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [April 19, 2006, 4:26pm UTC](https://www.ruby-forum.com/t/weird-problem-probably-a-noob-mistake/58199/1 "2006-04-19T16:26:00Z")

</div>

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:

## \<%=@bio.name%\>

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 
## \<%=@bio.type%\>
 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?

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [April 19, 2006, 4:30pm UTC](https://www.ruby-forum.com/t/weird-problem-probably-a-noob-mistake/58199/2 "2006-04-19T16:30:16Z")

</div>

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:
> 
> ## \<%=@bio.name%\>
> 
> 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

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [April 19, 2006, 4:35pm UTC](https://www.ruby-forum.com/t/weird-problem-probably-a-noob-mistake/58199/3 "2006-04-19T16:35:00Z")

</div>

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:
> 
> ## \<%=@bio.name%\>
> 
> I get an error:  
> undefined method `name’ for #Array:0x3892618

A few things 🙂

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

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [April 19, 2006, 4:36pm UTC](https://www.ruby-forum.com/t/weird-problem-probably-a-noob-mistake/58199/4 "2006-04-19T16:36:43Z")

</div>

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’.

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [April 19, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/weird-problem-probably-a-noob-mistake/58199/5 "2006-04-19T16:34:51Z")

</div>

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

> However when I attempt to display the data in the view using this:
> 
> ## \<%=@bio.name%\>
> 
> 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
