# Newbie, needs help, can someone explain

**URL:** <https://www.ruby-forum.com/t/newbie-needs-help-can-someone-explain/145963>\
**Category:** Rails\
**Created:** [August 15, 2008, 3:48pm UTC](https://www.ruby-forum.com/t/newbie-needs-help-can-someone-explain/145963 "2008-08-15T15:48:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![brads](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecccb3/32.png) [@brads](https://www.ruby-forum.com/u/brads)\
**Post date:** [August 15, 2008, 3:48pm UTC](https://www.ruby-forum.com/t/newbie-needs-help-can-someone-explain/145963/1 "2008-08-15T15:48:52Z")

</div>

Sometimes when i am trying to use an instance variable in my view, which  
is created in my controller, the value for the :id when passing as part  
of the URL, just appears as an obscure number 23743276

is there a standard format to format this so it appears as the value in  
the DB.

i dont understand how this works?

---

<div class="post-metadata">

**Author:** ![brads](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecccb3/32.png) [@brads](https://www.ruby-forum.com/u/brads)\
**Post date:** [August 15, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/newbie-needs-help-can-someone-explain/145963/2 "2008-08-15T15:55:08Z")

</div>

Brad S. wrote:

> Sometimes when i am trying to use an instance variable in my view, which  
> is created in my controller, the value for the :id when passing as part  
> of the URL, just appears as an obscure number 23743276
> 
> is there a standard format to format this so it appears as the value in  
> the DB.
> 
> i dont understand how this works?

for instance

this @testrun = Testrun.find\_all\_by\_id(:last)

in the view im referencing is by: @testrun.id

it should be 916 in the db, but it just appears as 3765348

???

---

<div class="post-metadata">

**Author:** ![brads](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecccb3/32.png) [@brads](https://www.ruby-forum.com/u/brads)\
**Post date:** [August 15, 2008, 4:00pm UTC](https://www.ruby-forum.com/t/newbie-needs-help-can-someone-explain/145963/3 "2008-08-15T16:00:23Z")

</div>

You are calling the method find\_all which returns a collection not an  
object. Leave out the all part. Also find\_by\_id is the default  
behavior of find so you can just type in Testrun.find(:last) or  
another shortcut, just Testrun.last.

On Aug 15, 9:55 am, Brad S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![brads](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecccb3/32.png) [@brads](https://www.ruby-forum.com/u/brads)\
**Post date:** [August 15, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/newbie-needs-help-can-someone-explain/145963/4 "2008-08-15T16:02:08Z")

</div>

Brad -

On 15-Aug-08, at 9:55 AM, Brad S. wrote:

> > the DB.
> 
> ???

.find\_all\_by\_id (hint: all) will be returning a collection

so @testrun is a collection (so the id you’re getting is for the  
collection object itself

@testrun.first.id will give you the id of the first element

or switch to Testrun.find(:last) # retrieve a single instance

Jodi

---

<div class="post-metadata">

**Author:** ![brads](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecccb3/32.png) [@brads](https://www.ruby-forum.com/u/brads)\
**Post date:** [August 15, 2008, 4:18pm UTC](https://www.ruby-forum.com/t/newbie-needs-help-can-someone-explain/145963/5 "2008-08-15T16:18:32Z")

</div>

If i use first it works fine and displays the first id in the table.

does it matter which version of rails im using, im using aptana running  
instant rails.

would that affect this?

---

<div class="post-metadata">

**Author:** ![brads](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecccb3/32.png) [@brads](https://www.ruby-forum.com/u/brads)\
**Post date:** [August 15, 2008, 4:17pm UTC](https://www.ruby-forum.com/t/newbie-needs-help-can-someone-explain/145963/6 "2008-08-15T16:17:33Z")

</div>

ActiveRecord::RecordNotFound in VersionController#show  
Couldn’t find Testrun with ID=last

error when using Testrun.find(:last)

when i used find\_by\_sql(“SELECT MAX(id) FROM TESTRUNS”)

I get

Testun328976%340ioh4%nrklh%Testrun38588y3%… … …

---

<div class="post-metadata">

**Author:** ![brads](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecccb3/32.png) [@brads](https://www.ruby-forum.com/u/brads)\
**Post date:** [August 15, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/newbie-needs-help-can-someone-explain/145963/7 "2008-08-15T17:30:43Z")

</div>

learc wrote:

> Also find\_by\_id is the default  
> behavior of find so you can just type in Testrun.find(:last) or  
> another shortcut, just Testrun.last.

This is incorrect. find\_by\_id is _not_ the same as find. If you call  
find for a non-existent id, Rails will raise an  
ActiveRecord::RecordNotFound error, whereas find\_by\_id will just return  
a nil. With find\_by\_id, you can do things like:

person = Person.find\_by\_id(params[:id]) || Person.new

I use that style to share code between :create and :update actions  
(where appropriate).

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![brads](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecccb3/32.png) [@brads](https://www.ruby-forum.com/u/brads)\
**Post date:** [August 15, 2008, 4:24pm UTC](https://www.ruby-forum.com/t/newbie-needs-help-can-someone-explain/145963/8 "2008-08-15T16:24:28Z")

</div>

Brad -

On 15-Aug-08, at 10:18 AM, Brad S. wrote:

> If i use first it works fine and displays the first id in the table.
> 
> does it matter which version of rails im using, im using aptana  
> running  
> instant rails.
> 
> would that affect this?

find(:last) was introduced after 2.0.2 (ie. :last works in 2.1.0)

If you consult the docs for instant\_rails it will tell you what  
version you installed (or run ‘ruby script/about’ inside the project  
folder)

Cheers,  
Jodi
