# Batch uploading data and using file\_column

**URL:** <https://www.ruby-forum.com/t/batch-uploading-data-and-using-file-column/71801>\
**Category:** Rails\
**Created:** [October 11, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/batch-uploading-data-and-using-file-column/71801 "2006-10-11T16:34:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@Clare](https://www.ruby-forum.com/u/Clare)\
**Post date:** [October 11, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/batch-uploading-data-and-using-file-column/71801/1 "2006-10-11T16:34:06Z")

</div>

Hi I have a site which is working quite nicely at the moment but i want  
to batch upload a large amount of data from a csv file. Up until now i  
have added new items by hand through the forms, now i have a script to  
do it. The script is fine up until i try to run the images through  
file\_column. The problem being that the images are stored as url’s. So  
question is how can i present to url’s to file column in a way that it  
understands how to follow them and get the files or alternatively how  
can i get the images pointed to by the url and then present them to  
file\_column as if they were uploaded data.

thanks alot  
regards  
c

---

<div class="post-metadata">

**Author:** ![Clare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@Clare](https://www.ruby-forum.com/u/Clare)\
**Post date:** [October 11, 2006, 5:26pm UTC](https://www.ruby-forum.com/t/batch-uploading-data-and-using-file-column/71801/2 "2006-10-11T17:26:18Z")

</div>

On 11 October 2006 18:34, Clare wrote:

> Hi I have a site which is working quite nicely at the moment but i want  
> to batch upload a large amount of data from a csv file. Up until now i  
> have added new items by hand through the forms, now i have a script to  
> do it. The script is fine up until i try to run the images through  
> file\_column. The problem being that the images are stored as url’s. So  
> question is how can i present to url’s to file column in a way that it  
> understands how to follow them and get the files or alternatively how  
> can i get the images pointed to by the url and then present them to  
> file\_column as if they were uploaded data.

I think the best way is to handle such cases manually and not alter the  
file\_column. Something like that:

require ‘open-uri’

uri = “[http://somewhere.example.com/myimage.gif](http://somewhere.example.com/myimage.gif)”

open(uri) do |remote|  
uploadedfile = StringIO.new(remote.read)  
end

filename = File.basename(uri)  
(class \<\< uploadedfile; self; end).class\_eval do  
define\_method(:local\_path) { “” }  
define\_method(:original\_filename) { filename }  
define\_method(:content\_type) { “application/octet-stream” }  
end

Then assign this uploadedfile to file\_column attribute.

---

<div class="post-metadata">

**Author:** ![Clare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@Clare](https://www.ruby-forum.com/u/Clare)\
**Post date:** [October 11, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/batch-uploading-data-and-using-file-column/71801/3 "2006-10-11T18:06:34Z")

</div>

Dear Maxim  
Thank you very much for replying but sadly I’m not yet good enough to  
understand your fancy code.

I used it as follows but recieced the following error:

Sorry i usually have a better crack at solving issues but I don’t  
understand what is going on in the section that begins (class \<\<  
uploadedfile; self; end).class\_eval. If you get time to reply that would  
be excellent, if not thanks for giving me something to investigate  
further.

regards  
c

NameError: undefined local variable or method `uploadedfile' for VoObject:Class from /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1129:in `method\_missing’

if imageLink.size \> 0  
uri =  
“[http://www.desperateseller.co.uk/DespSellPhotos/101946825798.jpg](http://www.desperateseller.co.uk/DespSellPhotos/101946825798.jpg)”  
open(uri) do |remote|  
uploadedfile = StringIO.new(remote.read)  
end

filename = File.basename(uri)  
(class \<\< uploadedfile; self; end).class\_eval do  
define\_method(:local\_path) { “” }  
define\_method(:original\_filename) { filename }  
define\_method(:content\_type) { “application/octet-stream” }  
end

vobject.primary\_image = uploadedfile  
end

Maxim K. wrote:

> On 11 October 2006 18:34, Clare wrote:
> 
> > Hi I have a site which is working quite nicely at the moment but i want  
> > to batch upload a large amount of data from a csv file. Up until now i  
> > have added new items by hand through the forms, now i have a script to  
> > do it. The script is fine up until i try to run the images through  
> > file\_column. The problem being that the images are stored as url’s. So  
> > question is how can i present to url’s to file column in a way that it  
> > understands how to follow them and get the files or alternatively how  
> > can i get the images pointed to by the url and then present them to  
> > file\_column as if they were uploaded data.
> 
> I think the best way is to handle such cases manually and not alter the  
> file\_column. Something like that:
> 
> require ‘open-uri’
> 
> uri = “[http://somewhere.example.com/myimage.gif](http://somewhere.example.com/myimage.gif)”
> 
> open(uri) do |remote|  
> uploadedfile = StringIO.new(remote.read)  
> end
> 
> filename = File.basename(uri)  
> (class \<\< uploadedfile; self; end).class\_eval do  
> define\_method(:local\_path) { “” }  
> define\_method(:original\_filename) { filename }  
> define\_method(:content\_type) { “application/octet-stream” }  
> end
> 
> Then assign this uploadedfile to file\_column attribute.

---

<div class="post-metadata">

**Author:** ![Clare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@Clare](https://www.ruby-forum.com/u/Clare)\
**Post date:** [October 12, 2006, 6:28am UTC](https://www.ruby-forum.com/t/batch-uploading-data-and-using-file-column/71801/4 "2006-10-12T06:28:17Z")

</div>

Clare wrote:

> Thank you very much for replying but sadly I’m not yet good enough to  
> understand your fancy code.
> 
> Sorry i usually have a better crack at solving issues but I don’t  
> understand what is going on in the section that begins (class \<\<  
> uploadedfile; self; end).class\_eval. If you get time to reply that would  
> be excellent, if not thanks for giving me something to investigate  
> further.  
> “class \<\< uploadedfile; self; end” is a common technique to get  
> singleton class for that very instance to be able to extend it. I think  
> it could be done in a more simple way, I just took a code from  
> file\_column’s test method that simulate an uploaded file.  
> In Rails, uploaded file is an IO object or the like (which has “read”  
> method) extended with some instance methods (local\_path,  
> original\_filename, content\_type).

Regarding your error, I think you haven’t required ‘open-uri’ library  
and this line  
uploadedfile = StringIO.new(remote.read)  
haven’t got executed. That’s probably why you’ve got “undefined  
variable” error. I suggest you to put all the code inside open(uri)  
block so you’ll ensure that nothing will happen if it is unable to open  
that uri.

---

<div class="post-metadata">

**Author:** ![Clare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@Clare](https://www.ruby-forum.com/u/Clare)\
**Post date:** [October 12, 2006, 8:30pm UTC](https://www.ruby-forum.com/t/batch-uploading-data-and-using-file-column/71801/5 "2006-10-12T20:30:19Z")

</div>

Thanks again,  
got it working  
really silly error

uploadedfile = nil

needs to be added before the open(uri) block.

really should have spotted that.  
cheers  
c
