How to work with data from DB in controller

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

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

On 1 April 2011 08:16, Manny 777 [email protected] 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

On 1 April 2011 00:00, Manny 777 [email protected] 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

Sorry, missed the nil part. What Colin said below is correct. Try that
first.

B.

Sent from my iPhone

On 1 April 2011 08:16, Manny 777 [email protected] 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… :-/

Sent from my iPhone

On Apr 1, 2011, at 2:16 AM, Manny 777 [email protected] 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.