Id of last element in a table

Hi,

I would like to figure out how to determine what the id of the last
element in a table is. Is there an equivalent to the Table.find() method
which will tell me the number of elements in a table? also, maybe more
importantly, where are the available methods on tables documented?

What i am trying to do is to increment the id and work through a table
and check whether the id i am currently on is the last in the table.

thanks for any help

Model_Name.find_all.last

and

the API is at

http://api.rubyonrails.com/

Roland M. wrote:

Model_Name.find_all.last

and

the API is at

http://api.rubyonrails.com/

thanks

On Monday, June 26, 2006, at 8:26 PM, Seamus Gilchrist wrote:


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

The previously mentioned approach will load all records into memory,
which could be a performance problem. Try this…

Model.find(:first, :order=>‘id DESC’)

or if you have a ‘created_at’ field, use

Model.find(:first, :order=>‘created_at DESC’

_Kevin

Kevin O. wrote:

On Monday, June 26, 2006, at 8:26 PM, Seamus Gilchrist wrote:


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

The previously mentioned approach will load all records into memory,
which could be a performance problem. Try this…

Model.find(:first, :order=>‘id DESC’)

or if you have a ‘created_at’ field, use

Model.find(:first, :order=>‘created_at DESC’

_Kevin

Kevin,

this seems to work ok - I saw looking at the sql documentation that
there is a sql call that you can make to find the id of the last
inserted elements in autoincrement tables -

… from mysql documentation

If you insert a record in a table containing a column that has the
AUTO_INCREMENT attribute, you can get the most recently generated ID by
calling the mysql_insert_id() function.

You can also retrieve the ID by using the LAST_INSERT_ID() function in a
query string that you pass to mysql_query().

You can check if an AUTO_INCREMENT index is used by executing the
following code. This also checks if the query was an INSERT with an
AUTO_INCREMENT index:

if (mysql_error(&mysql)[0] == 0 &&
mysql_num_fields(result) == 0 &&
mysql_insert_id(&mysql) != 0)
{
used_id = mysql_insert_id(&mysql);
}

is there any way to invoke this function directly from rails on the
table? would it be more efficient to do this?

thanks

On Monday, June 26, 2006, at 8:26 PM, Seamus Gilchrist wrote:


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

The previously mentioned approach will load all records into memory,
which could be a performance problem. Try this…

Model.find(:first, :order=>‘id DESC’)

or if you have a ‘created_at’ field, use

Model.find(:first, :order=>‘created_at DESC’

_Kevin

On Tuesday 27 June 2006 13:37, Seamus Gilchrist wrote:

The previously mentioned approach will load all records into memory,
which could be a performance problem. Try this…

Model.find(:first, :order=>‘id DESC’)

or if you have a ‘created_at’ field, use

Model.find(:first, :order=>‘created_at DESC’

This sounds like the best solution to your problem.

this seems to work ok - I saw looking at the sql documentation that
there is a sql call that you can make to find the id of the last
inserted elements in autoincrement tables -
[…]
You can also retrieve the ID by using the LAST_INSERT_ID() function in
a query string that you pass to mysql_query().
[…]
is there any way to invoke this function directly from rails on the
table? would it be more efficient to do this?

It sounds like you are trying to find a pretty ass-backwards solution to
a
simple problem :slight_smile:
Actually I’d even reexamine the problem: If your app has any amount of
parallelism, the “last record in the table” right now might not be the
last record in the table two milliseconds afterwards. So – why exactly
do you want to know if the record is the last one?

On Tuesday 27 June 2006 16:00, Seamus Gilchrist wrote:

Actually, this particular table in the database is static and will not
change over time. So, there is no issue of parallelism here. I am
surprised that there is no method which corresponds directly to the
last_insert but if the Model.find(:first, :order=>‘id DESC’) is the
appropriate way and an efficient way to do this - ok! that’s what we’ll
do.

It definitely is. LAST_INSERT_ID is basically only intended for use
immediately after you inserted a row, to allow you to know the
(auto-incremented) primary key the row was inserted with.

Christian R. wrote:

On Tuesday 27 June 2006 13:37, Seamus Gilchrist wrote:

The previously mentioned approach will load all records into memory,
which could be a performance problem. Try this…

Model.find(:first, :order=>‘id DESC’)

or if you have a ‘created_at’ field, use

Model.find(:first, :order=>‘created_at DESC’

This sounds like the best solution to your problem.

this seems to work ok - I saw looking at the sql documentation that
there is a sql call that you can make to find the id of the last
inserted elements in autoincrement tables -
[…]
You can also retrieve the ID by using the LAST_INSERT_ID() function in
a query string that you pass to mysql_query().
[…]
is there any way to invoke this function directly from rails on the
table? would it be more efficient to do this?

It sounds like you are trying to find a pretty ass-backwards solution to
a
simple problem :slight_smile:
Actually I’d even reexamine the problem: If your app has any amount of
parallelism, the “last record in the table” right now might not be the
last record in the table two milliseconds afterwards. So – why exactly
do you want to know if the record is the last one?

Actually, this particular table in the database is static and will not
change over time. So, there is no issue of parallelism here. I am
surprised that there is no method which corresponds directly to the
last_insert but if the Model.find(:first, :order=>‘id DESC’) is the
appropriate way and an efficient way to do this - ok! that’s what we’ll
do.

thanks

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