:last Question

I’m new to rails and am trying to get an understanding on some of the
language usage…I have a mysql database with a table of data…I want
to retrieve the last record in that table without sending it an ‘id’
or anything…I’ve tried the following:

def get_last_net_vol
@prevticket = Tankticket.find(:last)
last_net_vol = @prevticket.net_vol
return last_net_vol
end

This fails with:

Couldn’t find Tankticket with ID=last

Extracted source (around line #54):

51:

VCF = <%= ctl -%>


52:

Net Vol = <%= round((grs_vol * ctl + roof_cor), 2) -%>


53:
54:

Last Net Vol = <%= get_last_net_vol -%>

Everything I’ve read tells me this should have worked so I’m
suspicious that it might be the version of rails I’m running…!

I’m running an old dual proc mac G4 with os x 10.5.8…ruby version
1.8.6…gem version 1.0.1…rails version 1.2.6

I know I should update to the later version of rails and gem…I have
tried but the standard methods to do so fail with
(Gem:RemoteSourceException) HTTP Response 301 fetching
RubyGems.org | your community gem host)…I guess I could install it the hard
way but I don’t want to chance compromising my current running
install…!

Any light shed will be greatly appreciated…!

Model.last

crashbangboom wrote:

I’m new to rails and am trying to get an understanding on some of the
language usage…I have a mysql database with a table of data…I want
to retrieve the last record in that table without sending it an ‘id’
or anything

Last by what definition? SQL databases return records in unpredictable
order unless you specify ORDER BY.

…I’ve tried the following:

def get_last_net_vol
@prevticket = Tankticket.find(:last)
last_net_vol = @prevticket.net_vol
return last_net_vol
end

This fails with:

Couldn’t find Tankticket with ID=last
[…]
I’m running an old dual proc mac G4 with os x 10.5.8…ruby version
1.8.6…gem version 1.0.1…rails version 1.2.6

Perhaps find(:last) didn’t exist in Rails 1.2.6. Even if it did, it’s
pretty meaningless without :order, and useless with :order – it would
be more efficient to sort the other way and use :first.

I know I should update to the later version of rails and gem…

Yes, you certainly should.

I have
tried but the standard methods to do so fail with
(Gem:RemoteSourceException) HTTP Response 301 fetching
RubyGems.org | your community gem host)…

What “standard methods”? What gave you this error?

I guess I could install it the hard
way but I don’t want to chance compromising my current running
install…!

Don’t be afraid, just install the upgrade. Either use “gem update
–system” (which has worked for me on Leopard) or download a new version
of gem.

Any light shed will be greatly appreciated…!

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

crashbangboom wrote:

I’m new to rails and am trying to get an understanding on some of the
language usage…I have a mysql database with a table of data…I want
to retrieve the last record in that table without sending it an ‘id’
or anything…I’ve tried the following:

def get_last_net_vol
@prevticket = Tankticket.find(:last)
last_net_vol = @prevticket.net_vol
return last_net_vol
end

Some problems I see with the above:

  1. Your find syntax is wrong.

    @prevticket = Tankticket.last

See the Rails guides for correct query usage:

  1. Without an ORDER BY clause, getting the first, or last record may not
    give you what you might expect.

Databases don’t always guarantee that records are returned in any
specific order. Again, look at the Rails guides to see the SQL generated
by ‘first’ and ‘last’ methods.

If you’re look for a specific record it is safer to specify the ORDER BY
clause. It happens that the last method does specify an ORDER BY.

Example:

Client.last
=> SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1

However first does not:

Client.first
=> SELECT * FROM clients LIMIT 1

In some database this could produce a result you might not expect. For
instance Oracle does not return results ordered by the primary key
without an ORDER BY clause specified explicitly. So Client.first will
give you back a single random row from the table. It will likely NOT be
the one with the smallest ID.

Specify your order explicitly if you really want the record with the
smallest or largest ID:

@prevticket = Tankticket.find(:first, :order => ‘id DESC’)

  1. You should NOT rely on the ordering of the primary key to find
    specific records. If you need to ensure that tank tickets are kept in a
    specific order then use a list management plugin. Something like
    acts_as_list (there are also some enhanced implementations of list
    management gems/plugins). Search Github.

  2. Try to use proper, and consistent naming conventions for your class
    and table names. Ruby used CamelCase for class names so each word in the
    class name should be capitalized.

Tankticket should be TankTicket.

Variable names should use underscores to separate words. They should be
all lower case and avoid abbreviations when non-obvious:

@prevticket should be @previous_ticket

I hope this is helpful and welcome to Rails.