Josh Pigford
2006-Mar-18 21:02 UTC
[Rails] 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. -- Posted via http://www.ruby-forum.com/.
Emin Hasanov
2006-Mar-18 21:46 UTC
[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 Pigford <jpigford@gmail.com> 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 > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060318/24a190a8/attachment.html
Josh Pigford
2006-Mar-18 21:58 UTC
[Rails] Re: How would I get the previous id from the database?
Emin Hasanov 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 databaseOkay...then how would i do that? :-) -- Posted via http://www.ruby-forum.com/.
Emin Hasanov
2006-Mar-18 22:12 UTC
[Rails] Re: How would I get the previous id from the database?
@image = Image.new ...your code here... @image.save then you can use @image.id as the unique name of the file On 3/19/06, Josh Pigford <jpigford@gmail.com> wrote:> > Emin Hasanov 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? :-) > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060318/c9709124/attachment.html
Bill Walton
2006-Mar-18 22:15 UTC
[Rails] How would I get the previous id from the database?
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 Hasanov To: rails@lists.rubyonrails.org 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 Pigford <jpigford@gmail.com> 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 Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails ------------------------------------------------------------------------------ _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060318/732be14a/attachment.html
Tom Mornini
2006-Mar-19 06:53 UTC
[Rails] How would I get the previous id from the database?
On Mar 18, 2006, at 2:00 PM, Bill Walton 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 thatThat 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. -- -- Tom Mornini -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060319/4a410fc1/attachment.html