Frederick Cheung wrote:> On 14 Aug 2008, at 15:11, John Griffiths wrote: > >> >> hope someone can help with this, >> >> in my mysql table i''ve got a blob column storing images inside the >> database, it''s also got the filetype and filesize. >> >> now i can work out what the filename will be (e.g. 2002.jpg) but i >> don''t >> know how to go thru each of the records and save the blob data to a >> file > > SomeClass.find(:all).each do |obj| > File.open(obj.filename, ''w'') do |f| > f.write obj.blob > end > end > > FredI am trying to save a blob column of MySQL database to a file with following code, but its not working: @attachment = Attachment.find(params[:id]) @filename = @attachment.filename File.open(File.join(RAILS_ROOT, "tmp", "#{@attachment.id}.doc"), "w") do |file| file.write(@attachment.data) end Can someone help me with this? Also, when are the contents of tmp directory deleted? Thanks, Vicky. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sep 24, 3:00 pm, Vicky Shaw <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Frederick Cheung wrote:> I am trying to save a blob column of MySQL database to a file with > following code, but its not working: > > @attachment = Attachment.find(params[:id]) > @filename = @attachment.filename > > File.open(File.join(RAILS_ROOT, "tmp", "#{@attachment.id}.doc"), > "w") do |file| > file.write(@attachment.data) > end > > Can someone help me with this?what''s happening or not happening? On some platforms you might need to change the mode to ''wb''> > Also, when are the contents of tmp directory deleted? >Never. RAILS_ROOT/tmp is just conceptually for temporary stuff. Apart from that it''s just a normal folder. For actual temporary files (as Tempfile might create) it depends on your platform (and in some cases on the system administration policies in effect on the machine. Fred --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Sep 24, 3:00�pm, Vicky Shaw <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> Frederick Cheung wrote: > >> >> Can someone help me with this? > > what''s happening or not happening? On some platforms you might need to > change the mode to ''wb'' >>I am using OpenSuse OS. I guess ''b'' is needed on Windows OS to save it as a binary file. Problem: I don''t see any such file on my filesystem.>> Also, when are the contents of tmp directory deleted? >> > Never. RAILS_ROOT/tmp is just conceptually for temporary stuff. Apart > from that it''s just a normal folder. For actual temporary files (as > Tempfile might create) it depends on your platform (and in some cases > on the system administration policies in effect on the machine. >So I may need to change my file path. I am saving these files for some further processing and then rendering. So I don''t want to store them permanently. Can someone suggest me a way to accomplish this task? Thanks for the prompt reply.. - Vicky> Fred-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 24 Sep 2008, at 15:52, Vicky Shaw wrote:> > Frederick Cheung wrote: >> On Sep 24, 3:00�pm, Vicky Shaw <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >> wrote: >>> Frederick Cheung wrote: >> >>> >>> Can someone help me with this? >> >> what''s happening or not happening? On some platforms you might need >> to >> change the mode to ''wb'' >>> > I am using OpenSuse OS. I guess ''b'' is needed on Windows OS to save it > as a binary file. > Problem: I don''t see any such file on my filesystem. >more likely that you don''t have write access or you''re ending up building a duff path. Inspect the exact path you''re trying to use and check permissions and so on. Fred>>> Also, when are the contents of tmp directory deleted? >>> >> Never. RAILS_ROOT/tmp is just conceptually for temporary stuff. Apart >> from that it''s just a normal folder. For actual temporary files (as >> Tempfile might create) it depends on your platform (and in some cases >> on the system administration policies in effect on the machine. >> > So I may need to change my file path. I am saving these files for some > further processing and then rendering. So I don''t want to store them > permanently. Can someone suggest me a way to accomplish this task? > > Thanks for the prompt reply.. > > - > Vicky > >> Fred > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 24 Sep 2008, at 15:52, Vicky Shaw wrote: > >>> to >>> change the mode to ''wb'' >>>> >> I am using OpenSuse OS. I guess ''b'' is needed on Windows OS to save it >> as a binary file. >> Problem: I don''t see any such file on my filesystem. >> > more likely that you don''t have write access or you''re ending up > building a duff path. Inspect the exact path you''re trying to use and > check permissions and so on.Thank you for the help Fred. The permissions were wrong. One more question: why is this error not logged in the rails log file? - Shantanu.> > Fred-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
The reason no error is thrown has nothing to do with Rails, but Ruby. IO write is the actual method that is being called, correct? See this: http://www.ruby-doc.org/core/classes/IO.html#M002296 According the the Ruby API is returns the number of bytes written. I would suggest you add a check on your write method that the number of bytes > 0 and log an error. H On Sep 25, 9:23 am, Vicky Shaw <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Frederick Cheung wrote: > > On 24 Sep 2008, at 15:52, Vicky Shaw wrote: > > >>> to > >>> change the mode to ''wb'' > > >> I am using OpenSuse OS. I guess ''b'' is needed on Windows OS to save it > >> as a binary file. > >> Problem: I don''t see any such file on my filesystem. > > > more likely that you don''t have write access or you''re ending up > > building a duff path. Inspect the exact path you''re trying to use and > > check permissions and so on. > > Thank you for the help Fred. > > The permissions were wrong. One more question: why is this error not > logged in the rails log file? > > - > Shantanu. > > > > > Fred > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---