# find(:last) just not working!

**URL:** <https://discuss.rubyonrails.org/t/find-last-just-not-working/33378>\
**Category:** rubyonrails-talk\
**Created:** [August 19, 2008, 11:57am UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378 "2008-08-19T11:57:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 19, 2008, 11:57am UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/1 "2008-08-19T11:57:37Z")

</div>

hi

i have just upgraded rails to the most recent version, and find(:last) is still not working!

any ideas?

heres the code in my controller: @testrun = Testrun.find(:last)

and heres the code in the view for test purposes: \<%= "#{@testrun.id}" -%\>

and heres the error: ActiveRecord::RecordNotFound in TestrunController#run

Couldn't find Testrun with ID=last

when i use @testrun = Testrun.find(:first), it works completely fine!

however i need to use last, tried a workaround @testrun = Testrun.find\_by\_sql("select max(id) from testruns"), but this gives a weird number ("29677700"), and not 16 which is the highest id in the testruns table!

any help would be most appreciated!

thanks in advance!

---

<div class="post-metadata">

**Author:** ![bphogan](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/bphogan/32/4186_2.png) [@bphogan](https://discuss.rubyonrails.org/u/bphogan)\
**Post date:** [August 19, 2008, 12:02pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/2 "2008-08-19T12:02:11Z")

</div>

Is this in an existing app? If so, did you change the Rails version used in environment.rb and then run rake rails:update to complete the update?

Anyway, to get you moving in the meantime, just do what :last does…

@last\_run = Testrun.find :first, :order =\> “id desc”

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 19, 2008, 12:06pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/3 "2008-08-19T12:06:29Z")

</div>

Try this: @testrun = Testrun.find(:all).last.id

---

<div class="post-metadata">

**Author:** ![Peter\_De\_Berdt](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@Peter\_De\_Berdt](https://discuss.rubyonrails.org/u/Peter_De_Berdt)\
**Post date:** [August 19, 2008, 12:07pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/4 "2008-08-19T12:07:03Z")

</div>

Indeed, or you have an old version frozen in your vendor folder.

Do script/console and see what it says.

liquid-snake:~/10-FW/RAILS/thinx peter$ script/console

Loading development environment ( **Rails 2.1.0** )

> > Company.find(:last)

=\> #\<Company id: 3, name: “Suez”, short\_name: “CC”, active: nil, language\_code: 2, created\_at: “2008-08-14 13:32:09”, updated\_at: “2008-08-14 13:32:09”\>

> > Company.last

=\> #\<Company id: 3, name: “Suez”, short\_name: “CC”, active: nil, language\_code: 2, created\_at: “2008-08-14 13:32:09”, updated\_at: “2008-08-14 13:32:09”\>

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 19, 2008, 12:09pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/5 "2008-08-19T12:09:23Z")

</div>

> Is this in an existing app? If so, did you change the Rails version used in environment.rb and then run rake rails:update to complete the update?

yes updated environment.tb with new version no, didnt run rake rails:update -\> now done it and command line returned quickly so not sure if any effect has taken place

running via aptana, so would i need to restart aptana too?

> @last\_run = Testrun.find :first, :order =\> "id desc"

thanks, will give this a try. much simpler with the :last option though!

cheers.!

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 19, 2008, 12:15pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/6 "2008-08-19T12:15:04Z")

</div>

RAILS\_GEM\_VERSION = '2.0.2' unless defined? RAILS\_GEM\_VERSION -\>updated

ran rake rails:update -\> all looks good C:\InstantRails\rails\_apps\tpt\>rake rails:update --trace (in C:/InstantRails/rails\_apps/tpt) \*\* Invoke rails:update (first\_time) \*\* Invoke rails:update:scripts (first\_time) \*\* Execute rails:update:scripts \*\* Invoke rails:update:javascripts (first\_time) \*\* Execute rails:update:javascripts \*\* Invoke rails:update:configs (first\_time) \*\* Execute rails:update:configs \*\* Execute rails:update

restart aptana, and still get the same problems??

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 19, 2008, 12:19pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/7 "2008-08-19T12:19:09Z")

</div>

Vidya Ramachandren wrote:

> Try this: @testrun = Testrun.find(:all).last.id

thanks, that works! but why doesnt (:last) work, is it a bug or something?

do you have any tips how to make a find\_by\_sql to work as well?

the syntax?

thanks again!

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 19, 2008, 12:24pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/8 "2008-08-19T12:24:15Z")

</div>

hi peter

where do i run the script/console command, via dos? and what exactly am i looking for? how can i determine if an old version is hanging around?

more information needed, im a newbie to rails.

thanks

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 19, 2008, 12:26pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/9 "2008-08-19T12:26:13Z")

</div>

> Couldn't find Testrun with ID=last
> 
> when i use @testrun = Testrun.find(:first), it works completely fine!
> 
> however i need to use last, tried a workaround @testrun = Testrun.find\_by\_sql("select max(id) from testruns"), but this gives a weird number ("29677700"), and not 16 which is the highest id in the testruns table!

That returns an array of objects, so when you called id on it you were  
getting the id of the array, not the id of the record. You don't need find\_by\_sql for this: Testrun.max(:id) should do the  
trick.

It really does sound as though you're just running 2.0.2. Did you  
check the output of script/console as suggested. Do you have any  
plugins that might be overriding find ?

Fred

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 19, 2008, 12:26pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/10 "2008-08-19T12:26:46Z")

</div>

> hi peter
> 
> where do i run the script/console command, via dos? and what exactly  
> am i looking for? how can i determine if an old version is hanging  
> around?

from your app's folder run ruby script/console

it outputs the version of rails it's running.

Fred

---

<div class="post-metadata">

**Author:** ![Peter\_De\_Berdt](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@Peter\_De\_Berdt](https://discuss.rubyonrails.org/u/Peter_De_Berdt)\
**Post date:** [August 19, 2008, 12:30pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/11 "2008-08-19T12:30:39Z")

</div>

> Try this: @testrun = Testrun.find(:all).last.id

That’s about the most horrible way of doing it: load the complete recordset into memory, then take the last one and get the id. The best way is to sort by id DESC and then take the first one. That’s what .last does anyway.

And by the looks of it, you’re still running Rails 2.0.2:

> RAILS\_GEM\_VERSION = ‘2.0.2’ unless defined? RAILS\_GEM\_VERSION -\>updated

The :last and named\_scope feature was introduced in Rails 2.1, which is the latest version, not Rails 2.0.2.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![radar](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/radar/32/604_2.png) [@radar](https://discuss.rubyonrails.org/u/radar)\
**Post date:** [August 19, 2008, 12:51pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/12 "2008-08-19T12:51:15Z")

</div>

Peter's correct.

You're trying to use a 2.0.2 Rails copy when you should be using a 2.1

---

<div class="post-metadata">

**Author:** ![Jorg\_Lueke](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jorg\_Lueke](https://discuss.rubyonrails.org/u/Jorg_Lueke)\
**Post date:** [August 19, 2008, 1:01pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/13 "2008-08-19T13:01:27Z")

</div>

find(:last) doesn't work for me in 2.0.2 it works fine in 2.1 find\_by\_sql works fine the way described in the api.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 19, 2008, 1:04pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/14 "2008-08-19T13:04:32Z")

</div>

> You're trying to use a 2.0.2 Rails copy when you should be using a 2.1

yes im running rails 2.0.2, in fact this is the most recent build packaged with instantrails 2.0.

can anyone recommend me to a good site for installing ruby/rails outside of instant rails because ive tried a few times, and it seems never to run smoothly, usually the RUBY\_ENV problems, not picking up the right things, and then GEM issues to.

??

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 19, 2008, 1:12pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/15 "2008-08-19T13:12:29Z")

</div>

In 2.0.2, the only method given above which works is:

@testrun = Testrun.find(:all).last.id

@testrun = Testrun.find :first, :order =\> "id desc

> > gives "#" in the view as the id

@testrun = Testrun.max(:id)

> > gives undefined method `max' for #\<Class:0x39d24e0\>

so the nasty method in 2.0.2 seems the be the only way forward, thanks, but until i can run rails outside of instantrails(which i believe is not being supported or developed anymore) i will have to stick with this.

thanks for all your help.

although still not sure the find\_by\_sql will work, as mentioned above, the 2357423 it returns is the array location or whatever, how do i then translate to give me the 18(the last record with the highest id) in the db.

using the find\_by\_sql("select max(id) from testruns)???

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 19, 2008, 1:15pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/16 "2008-08-19T13:15:23Z")

</div>

Jorg Lueke wrote:

> gem install update rails doesn't work?

i cant run it, as my company has a download firewall, so the aptana auto updater cant get any access to the outside world to update.

and i tried to install the gem manually, but i had a bit of trouble, via the zip, where and how do i do that?

i extracted it into the gems folder, but for some reason it wasnt being picked up!

thanks for your help!

---

<div class="post-metadata">

**Author:** ![Jorg\_Lueke](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jorg\_Lueke](https://discuss.rubyonrails.org/u/Jorg_Lueke)\
**Post date:** [August 19, 2008, 1:15pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/17 "2008-08-19T13:15:27Z")

</div>

Did you try gem update rails and updating your version in any existing environment.rb?

---

<div class="post-metadata">

**Author:** ![Peter\_De\_Berdt](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@Peter\_De\_Berdt](https://discuss.rubyonrails.org/u/Peter_De_Berdt)\
**Post date:** [August 19, 2008, 1:17pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/18 "2008-08-19T13:17:20Z")

</div>

Make it easy on yourself and make either a virtual machine running linux or make a linux/windows dual boot (Windows for gaming and adware/virus fun, Linux for real productivity). I would recommend Ubuntu if you’re not familiar with Linux, because the latest version is just such a smooth install (better than a Winblows install even, not too hard, but you get the point). You’ll have far less problems with gems too.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 19, 2008, 1:21pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/19 "2008-08-19T13:21:24Z")

</div>

> In 2.0.2, the only method given above which works is:
> 
> @testrun = Testrun.find(:all).last.id
> 
> @testrun = Testrun.find :first, :order =\> "id desc
> 
> > > gives "#" in the view as the id

that's because @testrun is an instance of Testrun, not an integer

@testrun = Testrun.find( :first, :order =\> "id desc").id would work (although you'll get an error there if there are no testruns)

> @testrun = Testrun.max(:id)
> 
> > > gives undefined method `max' for #\<Class:0x39d24e0\>

D'oh the method is maximum not max.

> so the nasty method in 2.0.2 seems the be the only way forward,  
> thanks, but until i can run rails outside of instantrails(which i believe is  
> not being supported or developed anymore) i will have to stick with this.
> 
> thanks for all your help.
> 
> although still not sure the find\_by\_sql will work, as mentioned above, the 2357423 it returns is the array location or whatever, how do i  
> then translate to give me the 18(the last record with the highest id) in  
> the db.
> 
> using the find\_by\_sql("select max(id) from testruns)???

you need to look at the first element of that array.

Fred

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 19, 2008, 1:31pm UTC](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378/20 "2008-08-19T13:31:04Z")

</div>

> Jorg Lueke wrote:
> 
> > gem install update rails doesn't work?
> 
> i cant run it, as my company has a download firewall, so the aptana auto updater cant get any access to the outside world to update.
> 
> and i tried to install the gem manually, but i had a bit of trouble, via the zip, where and how do i do that?

You can download the actual .gem file and install that ie

gem install something.gem

Fred

[Next page](https://discuss.rubyonrails.org/t/find-last-just-not-working/33378.md?page=2)
