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!
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?
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 ?
On 19 Aug 2008, at 14:06, Vidya Ramachandren wrote:
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:
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.
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)???
things, and then GEM issues to
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.