Forum: Ruby on Rails How would I get the previous id from the database?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Josh P. (Guest)
on 2006-03-18 23:01
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.
Emin H. (Guest)
on 2006-03-18 23:49
(Received via mailing list)
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
Josh P. (Guest)
on 2006-03-18 23:58
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? :-)
Emin H. (Guest)
on 2006-03-19 00:13
(Received via mailing list)
@image = Image.new
...your code here...
@image.save

then you can use @image.id as the unique name of the file
Bill W. (Guest)
on 2006-03-19 00:16
(Received via mailing list)
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> 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
Tom M. (Guest)
on 2006-03-19 13:17
(Received via mailing list)
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.
This topic is locked and can not be replied to.