Newbie, needs help, can someone explain

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?

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

???

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. [email protected]

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

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?

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%… … …

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

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