How would I get the previous id from the database?

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.

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

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? :slight_smile:

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

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

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.

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: [email protected]
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. [email protected] 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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails