# How would I get the previous id from the database?

**URL:** <https://www.ruby-forum.com/t/how-would-i-get-the-previous-id-from-the-database/83719>\
**Category:** Rails\
**Created:** [March 18, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/how-would-i-get-the-previous-id-from-the-database/83719 "2006-03-18T22:01:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [March 18, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/how-would-i-get-the-previous-id-from-the-database/83719/1 "2006-03-18T22:01:45Z")

</div>

I’m trying to get the id of the previous row prior to what would be  
input and then rename an uploaded image to that row number.

So say the previous row was 19 and the image in that row was named  
19.jpg, I’m wanting to name the next uploaded image as 20.jpg since the  
row that would be added then would be 20.

Right now I’ve just got:

@new\_tutorial = Tutorial.new(params[:tutorial])  
File.open("#{RAILS\_ROOT}/public/pictures/1.jpg", “wb”) do |f|  
f.write(@params[‘image\_url’].read)  
end

Obviously the “1” in “1.jpg” needs to be dynamic.

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [March 18, 2006, 10:49pm UTC](https://www.ruby-forum.com/t/how-would-i-get-the-previous-id-from-the-database/83719/2 "2006-03-18T22:49:22Z")

</div>

actually, you need to do it other way around - add record to database  
and  
then use its id to save the file

of course you need to track errors in case saving files and you need to  
delete record from the database

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [March 18, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/how-would-i-get-the-previous-id-from-the-database/83719/3 "2006-03-18T22:58:21Z")

</div>

Emin H. wrote:

> actually, you need to do it other way around - add record to database  
> and  
> then use its id to save the file
> 
> of course you need to track errors in case saving files and you need to  
> delete record from the database

Okay…then how would i do that? 🙂

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [March 18, 2006, 11:13pm UTC](https://www.ruby-forum.com/t/how-would-i-get-the-previous-id-from-the-database/83719/4 "2006-03-18T23:13:45Z")

</div>

@image = Image.new  
…your code here…  
@image.save

then you can use @image.id as the unique name of the file

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [March 19, 2006, 12:17pm UTC](https://www.ruby-forum.com/t/how-would-i-get-the-previous-id-from-the-database/83719/5 "2006-03-19T12:17:02Z")

</div>

On Mar 18, 2006, at 2:00 PM, Bill W. wrote:

> A couple of (ok, three) cases to think about, if you haven’t already.
> 
> Do you need the previous record? (i.e., could have been stored by  
> any user)
> 
> - put a timestamp field in the record and query on that

That is _not_ unique and not recommended. It’ll  
apparently work like a charm under light loads,  
and fail miserably under high load.

The correct answer to your entire problem (though  
not the answer to the question you asked) has  
already been given.

Store the object first, then use it’s ID to name  
the photo.

Many people are not fans of the technique, but  
this sort of thing is unnecessary if you store  
the photo in the DB.

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [March 18, 2006, 11:16pm UTC](https://www.ruby-forum.com/t/how-would-i-get-the-previous-id-from-the-database/83719/6 "2006-03-18T23:16:49Z")

</div>

Hi Josh,

A couple of (ok, three) cases to think about, if you haven’t already.

Do you need the previous record? (i.e., could have been stored by any  
user)

- put a timestamp field in the record and query on that

The previous record stored by the current user, independent of whether  
it was during the current session?

- put a timestamp and user id in the record and query on that

Or the last record stored by the current user during the current  
session?

- put the record id in your session hash and retrieve it from there

Best regards,  
Bill  
----- Original Message -----  
From: Emin H.  
To: removed\_email\_address@domain.invalid  
Sent: 2006-03-18 3:46 PM  
Subject: Re: [Rails] How would I get the previous id from the  
database?

actually, you need to do it other way around - add record to database  
and then use its id to save the file

of course you need to track errors in case saving files and you need  
to delete record from the database

On 3/19/06, Josh P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
I’m trying to get the id of the previous row prior to what would be  
input and then rename an uploaded image to that row number.

```
So say the previous row was 19 and the image in that row was named
19.jpg, I'm wanting to name the next uploaded image as 20.jpg since 

```

the  
row that would be added then would be 20.

```
Right now I've just got:

@new_tutorial = Tutorial.new(params[:tutorial])
    File.open("#{RAILS_ROOT}/public/pictures/1.jpg", "wb") do |f|
      f.write(@params['image_url'].read)
    end

Obviously the "1" in "1.jpg" needs to be dynamic.

--
Posted via http://www.ruby-forum.com/ .
_______________________________________________
Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

```

* * *

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
