find(:last) just not working!

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!

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”

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

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”>

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.!

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??

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!

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

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

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

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

Peter's correct.

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

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.

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.

??

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

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!

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

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

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

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