Hi, Im using find_by_sql to get a user''s account information and I get this error: NoMethodError in Account#view Showing *app/views/account/view.rhtml* where line *#9* raised: undefined method `LastName'' for #<Array:0x3652330> Here is my find_by_sql that I execute: find_by_sql ["select id, FirstName, LastName, WHERE id = ?", id] Why wouldn''t this work? I know I can just use find(id) but I''m trying to understand if this is a bug or if this is intended. Thanks, Jin _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
find_by_sql returns an array so it is different than find(id) because find(id) just returns a single object and not an array of objects. You have an error in your view because you are trying to access the LastName method of the array class (which doesn''t exist) that was returned from find_by_sql. -Jonathan On Nov 1, 2005, at 1:05 PM, Jin Lee wrote:> Hi, > > Im using find_by_sql to get a user''s account information and I get > this error: > > NoMethodError in Account#view > > Showing app/views/account/view.rhtml where line #9 raised: > > undefined method `LastName'' for #<Array:0x3652330> > > Here is my find_by_sql that I execute: > > find_by_sql ["select id, FirstName, LastName, WHERE id = ?", id] > > Why wouldn''t this work? I know I can just use find(id) but I''m > trying to understand if this is a bug or if this is intended. > > > Thanks, > > Jin > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks for the info, Jonathan. So if I am getting this straight, if I need to access this data in my views, I would do: @user = User.find_by_sql (....) @user[''LastName''] Or is there some way to still be able to access it via @user.LastName? Thanks again. Jin On 11/1/05, Jonathan Younger <daikini-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > find_by_sql returns an array so it is different than find(id) because > find(id) just returns a single object and not an array of objects. > You have an error in your view because you are trying to access the > LastName method of the array class (which doesn''t exist) that was returned > from find_by_sql. > > -Jonathan > > On Nov 1, 2005, at 1:05 PM, Jin Lee wrote: > > Hi, > > Im using find_by_sql to get a user''s account information and I get this > error: > > NoMethodError in Account#view > > Showing *app/views/account/view.rhtml* where line *#9* raised: > > undefined method `LastName'' for #<Array:0x3652330> > > Here is my find_by_sql that I execute: > > find_by_sql ["select id, FirstName, LastName, WHERE id = ?", id] > > Why wouldn''t this work? I know I can just use find(id) but I''m trying to understand if this is a bug or if this is intended. > > > Thanks, > > Jin > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Oh wait, nevermind, I get it now =) @user_array = User.find_by_sql(...) @user = @user_array[0] sorry, caffeine leaving body...... Thanks again, Jin On 11/1/05, Jin Lee <jinslee-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Thanks for the info, Jonathan. > > So if I am getting this straight, if I need to access this data in my > views, I would do: > > @user = User.find_by_sql (....) > > @user[''LastName''] > > Or is there some way to still be able to access it via @user.LastName? > > Thanks again. > > Jin > > On 11/1/05, Jonathan Younger <daikini-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > find_by_sql returns an array so it is different than find(id) because > > find(id) just returns a single object and not an array of objects. > > You have an error in your view because you are trying to access the > > LastName method of the array class (which doesn''t exist) that was returned > > from find_by_sql. > > > > -Jonathan > > > > On Nov 1, 2005, at 1:05 PM, Jin Lee wrote: > > > > Hi, > > > > Im using find_by_sql to get a user''s account information and I get this > > error: > > > > NoMethodError in Account#view > > > > Showing *app/views/account/view.rhtml * where line *#9* raised: > > > > undefined method `LastName'' for #<Array:0x3652330> > > > > Here is my find_by_sql that I execute: > > > > find_by_sql ["select id, FirstName, LastName, WHERE id = ?", id] > > > > > > Why wouldn''t this work? I know I can just use find(id) but I''m trying to understand if this is a bug or if this is intended. > > > > > > Thanks, > > > > Jin > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jin: I had the same trouble because find first produces one array (a row) but the find_by_sql seems to produce an array of arrays (a table). I had to use this format: @users.[0].LastName (if I were seeking the first row) or @users. [n].Last Name. Hope that helps. bruce On 1-Nov-05, at 5:57 PM, Jin Lee wrote:> Thanks for the info, Jonathan. > > So if I am getting this straight, if I need to access this data in > my views, I would do: > > @user = User.find_by_sql (....) > > @user[''LastName''] > > Or is there some way to still be able to access it via @user.LastName? > > Thanks again. > > Jin > > On 11/1/05, Jonathan Younger <daikini-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > find_by_sql returns an array so it is different than find(id) > because find(id) just returns a single object and not an array of > objects. > > You have an error in your view because you are trying to access the > LastName method of the array class (which doesn''t exist) that was > returned from find_by_sql. > > -Jonathan > > On Nov 1, 2005, at 1:05 PM, Jin Lee wrote: > >> Hi, >> >> Im using find_by_sql to get a user''s account information and I get >> this error: >> >> NoMethodError in Account#view >> >> Showing app/views/account/view.rhtml where line #9 raised: >> >> undefined method `LastName'' for #<Array:0x3652330> >> >> Here is my find_by_sql that I execute: >> >> find_by_sql ["select id, FirstName, LastName, WHERE id = ?", id] >> >> >> Why wouldn''t this work? I know I can just use find(id) but I''m >> trying to understand if this is a bug or if this is intended. >> >> >> Thanks, >> >> Jin >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails