id upper bounds

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.

You can call a find on your model, assuming you just want the highest id from that table:

last_id = MyModel.find(:first, :order => 'id desc').id

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

Franz Strebel wrote:

last_id = MyModel.find(:first, :order => 'id desc').id

@Franz

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

Eric B. wrote:

Franz Strebel wrote:

last_id = MyModel.find(:first, :order => 'id desc').id

@Franz

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.

:all or :first and :limit => 3

my_models = MyModel.find(:all, :order => 'id desc', :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

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.

To find the last id of MyModel you could do

MyModel.last.id

even souds like it should )

But it’s not what you really want for upper bound. In your case

MyModel.count(:all) or just MyModel.count

will fit better

? That's not the same. If I have a DB that assigns IDs sequentially, and I've created 5 instances, the last id will be 5.

If I subsequently destroy an instance, e.g. id == 1, the last id is still 5, but the count is 4.

They’re not the same, right.

I mean, it depends on what you need. The real ID or just an amount of records.

But If task is to get random record you will

Company.all[rand(Company.count)] but not

Company.all[rand(Company.last.id)]

I assume you meant find rather than all.

Neither of these will work in the face of deleted records, picking a random record is a bit tricky.

One way, which might not work for all databases, but assuming a mysql database might be:

Company.first( :order => "Rand()")

But this isn't very efficient and might not work for tables with lots of records.

Another approach is something like.

Company.first(:conditions => ["id <= ?", rand(Company.last.id) + 1]

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.