Forum: Ruby on Rails read last entry from data base

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F2b6d7bf3a3d324a5d0fb4894923d372?d=identicon&s=25 xxmithila (Guest)
on 2008-10-30 12:43
(Received via mailing list)
Hi..

 I want to read ID of last entered data set from table, are there any
possible ways to it?
help Im stucked.....
thankx
712ea4ed53c324183dddceadb6efb35d?d=identicon&s=25 Bala kishore Pulicherla (balakishore)
on 2008-10-30 12:45
(Received via mailing list)
try
Model.find(:last)
5acf8e6be07793d22e24d8eb24a0e043?d=identicon&s=25 reHa (Guest)
on 2008-10-30 12:48
(Received via mailing list)
or:

User.find(:all, :order => 'id DESC', :limit=>1)

On Oct 30, 12:44 pm, "bala kishore pulicherla" <balumc...@gmail.com>
C4fee92c07e4fee216aeab5be40e6cc9?d=identicon&s=25 Naga harish Kanegolla (nagaharish)
on 2008-10-30 13:12
If you want only the id of the last entered data then try this,
 User.maximum('id')
This will return the maximum id which will be anyways the last one.

xxmithila wrote:
> Hi..
>
>  I want to read ID of last entered data set from table, are there any
> possible ways to it?
> help Im stucked.....
> thankx
F2b6d7bf3a3d324a5d0fb4894923d372?d=identicon&s=25 mithila karunarathna (Guest)
on 2008-10-30 13:26
(Received via mailing list)
thankx guys
but
still there is a problem

User.find(:all, :order => 'id DESC', :limit=>1)

and

Model.find(:last)

both return "#"  mark only. whats the reson?  i want last index id
712ea4ed53c324183dddceadb6efb35d?d=identicon&s=25 Bala kishore Pulicherla (balakishore)
on 2008-10-30 13:29
(Received via mailing list)
ok then say

lst_id = Model.find(:last).id
puts lst_id.inspect

hope 'll get this

as Model.find_last retunrns the entire last record

On Thu, Oct 30, 2008 at 5:53 PM, mithila karunarathna
<xxmithilaxx@gmail.com
F2b6d7bf3a3d324a5d0fb4894923d372?d=identicon&s=25 mithila karunarathna (Guest)
on 2008-10-30 13:30
(Received via mailing list)
Thankx guys I got the answer....
WoW....
answer was

 Model.maximum("id")

thank u again for ur help
This topic is locked and can not be replied to.