I am new to both ruby and rails and a recent project I am trying has got
me working on it. I am trying to find a nice easy lightweight way to
find the last 'id' in a mysql database so I can take it and use it as
the upper bound in a random number generator.
My problem is that I haven't found an easy way to do this, but my only
other idea is to do something like a do loop that puts the ids into an
array and then grab the upper bound from that and that seems like a bad
idea.
That depends on the Datatype of your ID column in the table. It is a Mysql specific question. By rails default these are signed integer columns which can hold any value between -2147483648 to 2147483647
Thanks this worked great well kinda, I'm confused on why there is the
'.id' at the end also how would I limit this to give me say the first 3
from the back of the list instead of
:all or :first and :limit => 3
didn't work because i am calling this in my 'MyModel.rb' file so in my
index.html.erb file its set for .each with a do loop and every other
thing i have tried has bombed
People may say RoR is fun and easy and at the beginning it is but
sometimes its really frustrating
Thanks this worked great well kinda, I'm confused on why there is the
'.id' at the end also how would I limit this to give me say the first 3
from the back of the list instead of
It seems obvious to me. Look at the variable used "last_id." Sending the
"id" message to an instance of an ActiveRecord object returns the "id"
value of the object.
didn't work because i am calling this in my 'MyModel.rb' file so in my
index.html.erb file its set for .each with a do loop and every other
thing i have tried has bombed
I don't quite follow you here without seeing your code.
People may say RoR is fun and easy and at the beginning it is but
sometimes its really frustrating
Programming is HARD! If anyone tries to tell you different then they are
either lying to you, or are just plain gifted in the art of writing
code. For most of us, however, it takes effort to learn.
When people say RoR is fun and easy, they mean in comparison to other
languages and frameworks.
This should always return a random record (unless the table is empty),
but the records won't be evenly distributed because the probability of
selecting a particular record is proportional to the difference
between it's id and the id of the preceding existing record if any, or
0.
To all that have helped out thank you I do have what i wanted to
accomplished though it may not be super efficient it does what i need
with no repeats from my tests here is what i have done.