Forum: Ruby on Rails Weird Problem - probably a noob mistake

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7b2a4ea7a2c486b1b0ceab3c6d4680d9?d=identicon&s=25 Joe Cairns (diregnome)
on 2006-04-19 16:26
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:
<h2> <%=@bio.name%> </h2>

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 <h2> <%=@bio.type%> </h2> 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?
Bd3a4af368439803aa37d318f9941a9f?d=identicon&s=25 David (Guest)
on 2006-04-19 16:30
Joe Cairns 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:
> <h2> <%=@bio.name%> </h2>
>
> 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
83b830496f0393e9d32da57d4d5d4803?d=identicon&s=25 Scott Matthewman (Guest)
on 2006-04-19 16:34
(Received via mailing list)
On 19 Apr 2006, at 15:26, Joe Cairns wrote:

> However when I attempt to display the data in the view using this:
> <h2> <%=@bio.name%> </h2>
>
> 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
4daf0b71d5d9a3882e583c0e72eaf5dc?d=identicon&s=25 Alan Francis (Guest)
on 2006-04-19 16:35
Joe Cairns 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:
> <h2> <%=@bio.name%> </h2>
>
> 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
7b2a4ea7a2c486b1b0ceab3c6d4680d9?d=identicon&s=25 Joe Cairns (diregnome)
on 2006-04-19 16:36
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'.
This topic is locked and can not be replied to.