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
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 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
if (mysql_error(&mysql) == 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?