Hi there, I have the collection with items "name", "surname", "sequence". When I''m saving next user to my collection, I need to get the highest value of "sequence" in my collection, this value increment and save to new user... but I don''t know, how to get the highest value stored in collection... But obviously I''m getting error message about undefined local variable sequence. I am trying this problem to solve following: -- high = Users.find(:order => ''sequence_at DESC'', :limit => 1) unless high.sequence.nil? || high.sequence == 0 save_seq += 1 else save_seq = 1 end @user_new = Users.new(:name => params[:name], :sequence => save_seq) I will be very glad for every help! Thanks, Manny -- Posted via http://www.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 1 April 2011 00:00, Manny 777 <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> But obviously I''m getting error message about undefined local variable > sequence.Are you sure you''re not getting "undefined local variable save_seq"? Did you cut/paste the code here, or did you re-type it (so is it *exactly* what''s in your controller, or could there be small differences?) It could be worth you setting save_seq: high = Users.find(:order => ''sequence_at DESC'', :limit => 1) save_seq = high.sequence unless high.sequence.nil? || high.sequence == 0 ...etc -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No no, the exactly error is "undefined method `sequence'' for nil:NilClass". It looks I am getting the database object in var "high", but I can''t to get individual items from this object (like "sequence" or "name"). When I am trying to print the sequence (puts high.sequence), I am getting error "undefined method `sequence'' for nil:NilClass". If I look to data stored in database, I see: { "_id" : ObjectId("4d94f53fd3d8496acd00000d"), "name" : "John", "surname" : "Smith", "level" : 0, "sequence" : 0 } So I am a bit confused, where could be fail -- Posted via http://www.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 1 April 2011 08:16, Manny 777 <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> No no, the exactly error is "undefined method `sequence'' for > nil:NilClass". > > It looks I am getting the database object in var "high", but I can''t to > get individual items from this object (like "sequence" or "name"). When > I am trying to print the sequence (puts high.sequence), I am getting > error "undefined method `sequence'' for nil:NilClass".That is not what you said the error was initially. High must be nil. Try surrounding the code by if !high.nil? (or just if high). That should make the error go away. Then you can work out why high is nil. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sent from my iPhone On Apr 1, 2011, at 2:16 AM, Manny 777 <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> No no, the exactly error is "undefined method `sequence'' for > nil:NilClass". > > It looks I am getting the database object in var "high", but I can''t to > get individual items from this object (like "sequence" or "name"). When > I am trying to print the sequence (puts high.sequence), I am getting > error "undefined method `sequence'' for nil:NilClass".This is because more than one record has been returned with your find or it returned you single record search as an array. You have to loop over the items in the collection "high" then you will be able to access each items method. B. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sorry, missed the nil part. What Colin said below is correct. Try that first. B. Sent from my iPhone On Apr 1, 2011, at 2:24 AM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 1 April 2011 08:16, Manny 777 <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> No no, the exactly error is "undefined method `sequence'' for >> nil:NilClass". >> >> It looks I am getting the database object in var "high", but I can''t to >> get individual items from this object (like "sequence" or "name"). When >> I am trying to print the sequence (puts high.sequence), I am getting >> error "undefined method `sequence'' for nil:NilClass". > > > That is not what you said the error was initially. > High must be nil. Try surrounding the code by if !high.nil? (or just > if high). That should make the error go away. Then you can work out > why high is nil. > > Colin > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 1 April 2011 08:16, Manny 777 <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> No no,I heard you the first time :-/> the exactly error is "undefined method `sequence'' for > nil:NilClass".Now you see, this is where full error messages help. The object you''re calling "sequence" on is nil, so something is failing higher up in the code when populating "high".> It looks I am getting the database object in var "high",How are you sure? Have you done a "puts high.inspect" or inserted a debugger breakpoint at that line?> get individual items from this object (like "sequence" or "name"). When > I am trying to print the sequence (puts high.sequence), I am getting > error "undefined method `sequence'' for nil:NilClass".right... and at the risk of labouring the point, that "puts high.sequence" line isn''t in your original code snippet, and you didn''t indicate which line was failing, so how are we supposed to be able to see when you''re "obviously ... getting error message"? :-/> If I look to data stored in database, I see: > > { "_id" : ObjectId("4d94f53fd3d8496acd00000d"), "name" : "John", > "surname" : "Smith", "level" : 0, "sequence" : 0 }How are you doing this? Show us the code that generates that result> So I am a bit confusedMe too... not least by the fact that your "Users.find" is plural (is the model *really* called "Users", or is it "User"?), and that the condition looks for "sequence_at" while everywhere else you ask just for "sequence". And finally, that if all you''re after is the highest value for "sequence" (or sequence_at) from your table, why not use the ActiveRecord calculation method "maximum"? high = User.maximum(:sequence) I sincerely hope that something here helps you, but first you have to help yourself by asking well thought-out questions, and answering any requests for clarification without tetchiness... :-/ -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.