Is there a simple command that will simply output the last id of my model/MySQL table as an integer? --- [This E-mail scanned for viruses by Declude Virus]
Michael Klein wrote:> Is there a simple command that will simply output the last id of my > model/MySQL table as an integer?Model.find(:first, :order => ''id DESC'').id jeremy
Why exactly do you need the number? Be very very careful of trusting any number you get back from querying the database. In a multi-user environment this "last id" can change without notice and get you in lots of trouble in a hurry that can be very messy to debug. On 4/26/05, Michael Klein <michaelklein-fliffHUUhfJg9hUCZPvPmw@public.gmane.org> wrote:> Is there a simple command that will simply output the last id of my > model/MySQL table as an integer?-- John W Higgins wishdev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
John Higgins <wishdev@...> writes:> > Why exactly do you need the number? Be very very careful of trusting > any number you get back from querying the database. In a multi-user > environment this "last id" can change without notice and get you in > lots of trouble in a hurry that can be very messy to debug. > > On 4/26/05, Michael Klein <michaelklein@...> wrote: > > Is there a simple command that will simply output the last id of my > > model/MySQL table as an integer? >I am trying to upload pictures for a photoblog I''m working on. I was thinking about just getting the upcoming id and naming the picture: "last id number".jpg or something. I didn''t realize that the last integer could change like that. I guess it would make more sense to just leave the uploaded file names as they are and store the path to the uploaded file. Thanks for your help and saving of a future headache!
> I am trying to upload pictures for a photoblog I''m working on. > > I was thinking about just getting the upcoming id and naming the picture: "last > id number".jpg or something. > > I didn''t realize that the last integer could change like that. I guess it would > make more sense to just leave the uploaded file names as they are and store the > path to the uploaded file. > > Thanks for your help and saving of a future headache!For your case what you need to do is save the object first, then use its ID, like so: post = photopost.new(@params[:photopost]) // Set other things here, like image size or whatever if (post.save) // I''m assuming you have the image in a variable called image, // the path to where photos are stored in imagePath and the // appropriate file extension (if any) in imageType. Oh, and that the // image object has a save method... image.save "#{imagePath}/#{post.id}.#{imageType} end As once saved the ID won''t change. -- Phillip Hutchings http://www.sitharus.com/ sitharus-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org / sitharus-QrR4M9swfipWk0Htik3J/w@public.gmane.org
Phillip Hutchings <sitharus@...> writes:> > For your case what you need to do is save the object first, then use > its ID, like so: > > post = photopost.new( <at> params[:photopost]) > // Set other things here, like image size or whatever > if (post.save) > // I''m assuming you have the image in a variable called image, > // the path to where photos are stored in imagePath and the > // appropriate file extension (if any) in imageType. Oh, and that the > // image object has a save method... > image.save "#{imagePath}/#{post.id}.#{imageType} > end > > As once saved the ID won''t change.Wow, thanks for this bit of code, that was exactly what I was going to work with next. I have the form and everything, just I can really upload the file yet. Will this image.save (i realize image is just a name you''ve given it) write directly to the hard drive in the given directory? And if so, is this directory the directory relative to my project''s public directory or the absolute path on my computer? I was thinking I would have to do something with FileUtils.incoming_file.copy or something like that.
Hey John, Maybe I''m doing something wrong. But after I save the object, the object.id is set to the autoinc value of field id of the table, or at last I thought. Is that what you are looking for? Joe -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of John Higgins Sent: Tuesday, April 26, 2005 2:00 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Determine the number of last id in table Why exactly do you need the number? Be very very careful of trusting any number you get back from querying the database. In a multi-user environment this "last id" can change without notice and get you in lots of trouble in a hurry that can be very messy to debug. On 4/26/05, Michael Klein <michaelklein-fliffHUUhfJg9hUCZPvPmw@public.gmane.org> wrote:> Is there a simple command that will simply output the last id of my > model/MySQL table as an integer?-- John W Higgins wishdev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 27.4.2005, at 00:49, Michael Klein wrote:> > Wow, thanks for this bit of code, that was exactly what I was going to > work with > next. I have the form and everything, just I can really upload the > file yet. > Will this image.save (i realize image is just a name you''ve given it) > write > directly to the hard drive in the given directory?No, it''s the normal save call for an ActiveRecord object (see http://rails.rubyonrails.com/classes/ActiveRecord/Base.html#M000688) so it will save the object in the database.> And if so, is this directory > the directory relative to my project''s public directory or the > absolute path on > my computer? I was thinking I would have to do something with > FileUtils.incoming_file.copy or something like that.See the Wiki for instructions on uploading/saving files to filesystem: http://wiki.rubyonrails.com/rails/show/HowtoUploadFiles //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jarkko Laine wrote:> No, it''s the normal save call for an ActiveRecord object (see > http://rails.rubyonrails.com/classes/ActiveRecord/Base.html#M000688) so > it will save the object in the database.I don''t think it is. I think Phillip''s example was to show the construction of a path from some other properties, specifically including the .id of an ActiveRecord object ("post", in the example). Image is presumably some other user-created type which would indeed save the file to disk.
On 27.4.2005, at 21:57, Kevin McConnell wrote:> Jarkko Laine wrote: > >> No, it''s the normal save call for an ActiveRecord object (see >> http://rails.rubyonrails.com/classes/ActiveRecord/Base.html#M000688) >> so it will save the object in the database. > > I don''t think it is. I think Phillip''s example was to show the > construction of a path from some other properties, specifically > including the .id of an ActiveRecord object ("post", in the example).You''re right, I was too hasty in reading the post. //jarkko> > Image is presumably some other user-created type which would indeed > save the file to disk. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 28/04/05, Kevin McConnell <kevin.mcconnell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Jarkko Laine wrote: > > > No, it''s the normal save call for an ActiveRecord object (see > > http://rails.rubyonrails.com/classes/ActiveRecord/Base.html#M000688) so > > it will save the object in the database. > > I don''t think it is. I think Phillip''s example was to show the > construction of a path from some other properties, specifically > including the .id of an ActiveRecord object ("post", in the example). > > Image is presumably some other user-created type which would indeed save > the file to disk.Sorry, I should have made it clear. image is just an image placeholder, it should be replace with a class that wraps up your image and saves it where ever you like, or just with the appropriate calls to whatever libraries you''re using. Personally, I''d add more sanitising to the upload - like relying on server-side detection for the appropriate image extension if you''re bothering with them. Never trust anything given to you, that''s how we end up with the various injection attacks. -- Phillip Hutchings http://www.sitharus.com/ sitharus-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org / sitharus-QrR4M9swfipWk0Htik3J/w@public.gmane.org