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.

On Mon, Apr 19, 2010 at 5:48 AM, Eric B. [email protected] wrote:

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.

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

Franz S. 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

On Mon, Apr 19, 2010 at 5:48 AM, Eric B. [email protected] wrote:

I am trying to find a nice easy lightweight way to
find the last ‘id’ in a mysql database

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

Eric B. wrote:

Franz S. 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

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)]

On Mon, Apr 19, 2010 at 11:29 PM, Hassan S. <

On Mon, Apr 19, 2010 at 9:03 AM, Vladimir R.
[email protected] wrote:

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.


Hassan S. ------------------------ [email protected]
twitter: @hassan

On Mon, Apr 19, 2010 at 12:47 PM, Vladimir R.
[email protected] wrote:

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.


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

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.

index.html.erb

<% @contents.each do |content| %>
    <div id="link"><a href="<%=h content.cont_url %>" title = "<%=h 
content.cont_url %>"><%=h(truncate(content.cont_url , 20)) %></a></div>
    <div id="comment"><%=h content.comments %></div>

<% end %>

content_controller.rb

def index
    @contents = Content.find_content

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @contents }
    end
  end
content.rb

def self.find_content
     find(:all, :limit => '3', :order => 'rand()')
end

If you have any questions or improvements or comments please let me know
thank you…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs