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:
I get an error:
undefined method `name’ for #Array:0x3892618
Here is the strange part:
- I saved @bio to the session and looked at the contents and it all
appears to be ok.
- If I change @bio = Bio.find(:all, :conditions => [‘site_id = ?’,
@session[‘site’].id]) to @bio = Bio.find(1) it works fine.
- I ran the SQL using @bio = Bio.find(:all, :conditions => [‘site_id =
?’, @session[‘site’].id]) and it works fine.
- When I display 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:
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
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:
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