Hi, i''m trying to get the results from find(). The code of the find is: @user = User.find(:all, :conditions => [ "user_id = ?", val ]) i''ve checked it with the function @user.size and it returns me one record. The problem is that inside @user i see only the character "#" and when i try to print some value of the record rails for example @user.nome, rails return me that error: undefined method `nome'' for #<Array:0xb68ba5e0> but nome is one column of the table!!!!! Can somebody help me??? thanks -- Posted via ruby-forum.com. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 6/30/07, incoroneta <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Hi, > > i''m trying to get the results from find(). The code of the find is: > > @user = User.find(:all, :conditions => [ "user_id = ?", val ]) > > i''ve checked it with the function @user.size and it returns me one > record. > > The problem is that inside @user i see only the character "#" and when i > try to print some value of the record rails for example @user.nome, > rails return me that error: > > undefined method `nome'' for #<Array:0xb68ba5e0> > > but nome is one column of the table!!!!! > > Can somebody help me??? > > thanksWhen you use find with the :all condition you are always returned an array. The returned array is a collection of Active Record objects or the resulting models from the database. @users = User.find(:all, :conditions => [ "user_id = ?", val ]) To get the first @users.first To get the last @users.last To go through each of them @users.each do |user| do_stuff end If your only after the first one then instead of using the :all option user the :first option. Instead of retuurning an array this will return an individual record as an ActiveRecord instance. HTH Daniel --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Daniel, i''ve tried both to use the first condition inside the find like this @user = User.find(:first, :conditions => [ "user_id = ?", val ]) and to take the result like @user.first but i''ve still the problem... The value inside @user is always # thanks -- Posted via ruby-forum.com. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
and to find out what your AR object looks like, you can do: <pre> <%= @users.inspect %> </pre> -- Posted via ruby-forum.com. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 6/30/07, incoroneta <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Hi Daniel, > > i''ve tried both to use the first condition inside the find like this > > @user = User.find(:first, :conditions => [ "user_id = ?", val ]) > > and to take the result like > > @user.first > > but i''ve still the problem... > > The value inside @user is always # > > thanksAs Dave said check what is in your @user variable with @user.inspect Can you post the actual controller code, and the view code please. It''s a little hard to guess what''s happening --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
i check the AR with @user.inspect ant the result is ok. the controller code is def show val=params[:id] @users = User.find(:all, :conditions => [ "user_id = ?", val ]) end and the view code is <% for column in User.content_columns %> <p> <b><%= column.human_name %>:</b> <%=h @user.send(column.name) %> </p> <% end %> <%= link_to ''Edit'', :action => ''edit'', :id => @user %> | <%= link_to ''Back'', :action => ''list'' %> the error undefined method `nome'' for #<Array:0xb68ba5e0> is given when its called @user.send(column.name) or also if you try to do flash[:notice] = @user.nome thanks -- Posted via ruby-forum.com. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 6/30/07, incoroneta <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > i check the AR with @user.inspect ant the result is ok. > > the controller code is > > def show > val=params[:id] > @users = User.find(:all, :conditions => [ "user_id = ?", val ]) > > end > > and the view code is > > <% for column in User.content_columns %> > <p> > <b><%= column.human_name %>:</b> <%=h @user.send(column.name) %> > </p> > <% end %> > > <%= link_to ''Edit'', :action => ''edit'', :id => @user %> | > <%= link_to ''Back'', :action => ''list'' %> > > > > > the error > undefined method `nome'' for #<Array:0xb68ba5e0> > is given when its called @user.send(column.name) or also if you try to > do > flash[:notice] = @user.nome > > thanksdef show @users = User.find(:all, :conditions => [ "user_id = ?", params[:id] ]) end You can change this to def show @users = User.find_all_by_id( params[:id] ) end It will be auto sanitized for you. You need to wrap up the view to run for each user. You''re passing it an array so you need to go through each element of it. <% @users.each do |user| %> <% for column in User.content_columns %> <p> <b><%= column.human_name %>:</b> <%=h user.send(column.name) %> </p> <% end %> <%= link_to ''Edit'', :action => ''edit'', :id => user %> | <%= link_to ''Back'', :action => ''list'' %> <% end %> That should do the trick for you. Please let me know if you don''t get anything there. HTH Daniel --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
hi Daniel, i tried and i get this error undefined method `find_all_by_id'' for User:Class i''ve to include some libraries?? thanks -- Posted via ruby-forum.com. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 6/30/07, incoroneta <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > hi Daniel, > > i tried and i get this error > > undefined method `find_all_by_id'' for User:Class > > i''ve to include some libraries?? > > thanks > > --mmm That''s strange. I''m able to use it. It''s a standard method. Have you had a look in console to see what''s going on? This is really helpful. Alternatively you could use a simple find_by_id User.find_by_id( params[:id] ) But this changes it a bit. You don''t need to go through an array now since it will provide either a user record or nil The controller action becomes def show @user = User.find_by_id( params[:id] ) end And the view <% unless @user.nil? %> <% for column in User.content_columns %> <p> <b><%= column.human_name %>:</b> <%=h @user.send(column.name) %> </p> <% end %> <%= link_to ''Edit'', :action => ''edit'', :id => @user %> | <%= link_to ''Back'', :action => ''list'' %> <% end %> <% end %> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
i''ve the same error.... undefined method `find_by_id'' for User:Class What do u mean for have a look in console?? how can i have a look in console to see what''s going on??? really thanks for the help!!!! -- Posted via ruby-forum.com. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 6/30/07, incoroneta <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > > i''ve the same error.... > > undefined method `find_by_id'' for User:Class > > What do u mean for have a look in console?? > > how can i have a look in console to see what''s going on??? > > really thanks for the help!!!!In your rails directory type ruby script/console This will give you a command prompt. You can type ruby in that will be executed in the rails environment so user = User.find( :first ) should return the first user in the database. Your user is an ActiveRecord class right? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
yes it is in ActiveRecord class. i''ll take a look and see what is happening... i''ll let u know thanks!! -- Posted via ruby-forum.com. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---