# :last Question

**URL:** <https://www.ruby-forum.com/t/last-question/192474>\
**Category:** Rails\
**Created:** [July 30, 2010, 8:50pm UTC](https://www.ruby-forum.com/t/last-question/192474 "2010-07-30T20:50:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![crashbangboom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9e8a1a/32.png) [@crashbangboom](https://www.ruby-forum.com/u/crashbangboom)\
**Post date:** [July 30, 2010, 8:50pm UTC](https://www.ruby-forum.com/t/last-question/192474/1 "2010-07-30T20:50:51Z")

</div>

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](http://gems.rubyforge.org/yaml))…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…!

---

<div class="post-metadata">

**Author:** ![crashbangboom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9e8a1a/32.png) [@crashbangboom](https://www.ruby-forum.com/u/crashbangboom)\
**Post date:** [July 30, 2010, 9:08pm UTC](https://www.ruby-forum.com/t/last-question/192474/2 "2010-07-30T21:08:25Z")

</div>

_Model.last_

> **[rails.info](http://guides.rails.info/active_record_querying.html)**
>
> Ruby on Rails,Rails Tutorial,Ruby Basics,Rails 4,Bar Rails,Rail

---

<div class="post-metadata">

**Author:** ![crashbangboom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9e8a1a/32.png) [@crashbangboom](https://www.ruby-forum.com/u/crashbangboom)\
**Post date:** [July 30, 2010, 9:12pm UTC](https://www.ruby-forum.com/t/last-question/192474/3 "2010-07-30T21:12:45Z")

</div>

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](http://gems.rubyforge.org/yaml))…

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![crashbangboom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9e8a1a/32.png) [@crashbangboom](https://www.ruby-forum.com/u/crashbangboom)\
**Post date:** [July 30, 2010, 9:42pm UTC](https://www.ruby-forum.com/t/last-question/192474/4 "2010-07-30T21:42:15Z")

</div>

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.

See the Rails guides for correct query usage:

> **[Active Record Query Interface — Ruby on Rails Guides](https://guides.rubyonrails.org/active_record_querying.html#retrieving-a-single-object)**
>
> Active Record Query InterfaceThis guide covers different ways to retrieve data from the database using Active Record.After reading this guide, you will know: How to find records using a variety of methods and conditions. How to specify the order,...

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.

> **[Camel case](https://en.wikipedia.org/wiki/Camelcase)**
>
> Camel case (sometimes stylized as camelCase or CamelCase, also known as camel caps or more formally as medial capitals) is the practice of writing phrases without spaces or punctuation and with capitalized words. The format indicates the first word starting with either case, then the following words having an initial uppercase letter. Common examples include "YouTube", "iPhone" and "eBay". Camel case is often used as a naming convention in computer programming. It is also sometimes used in onl...

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.
