Ruby on Rails Friday, April 1, 2011

On 1 April 2011 08:16, Manny 777 <lists@ruby-forum.com> 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 confused

Me 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@googlegroups.com.
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.

No comments:

Post a Comment