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? -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.
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
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 -- Posted via http://www.ruby-forum.com/.
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) > endArrggh, 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''. -- Posted via http://www.ruby-forum.com/.