# Can someone explain this code to me?

**URL:** <https://www.ruby-forum.com/t/can-someone-explain-this-code-to-me/88824>\
**Category:** Rails\
**Created:** [March 14, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/can-someone-explain-this-code-to-me/88824 "2007-03-14T19:38:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![treebeard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@treebeard](https://www.ruby-forum.com/u/treebeard)\
**Post date:** [March 14, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/can-someone-explain-this-code-to-me/88824/1 "2007-03-14T19:38:30Z")

</div>

taken from here using acts\_as\_fu, possible work for acts as attachment  
as well  
[http://beast.caboo.se/forums/2/topics/870](http://beast.caboo.se/forums/2/topics/870)

Image.transaction do  
images = params[:image].collect { |img| Image.create(:uploaded\_data =\>  
img }  
end unless params[:image].nil?

basically i think i can have a form with a dynamic amount of file fields  
for each file i would like to upload. Image i guess is the table that  
will be storing the photos.

i am confused at the block part. basically is this converting the hash  
into an array, going through each index in the array, and saving each  
photo into the Image table?

---

<div class="post-metadata">

**Author:** ![treebeard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@treebeard](https://www.ruby-forum.com/u/treebeard)\
**Post date:** [March 14, 2007, 7:52pm UTC](https://www.ruby-forum.com/t/can-someone-explain-this-code-to-me/88824/2 "2007-03-14T19:52:08Z")

</div>

treebeard wrote:

> taken from here using acts\_as\_fu, possible work for acts as attachment  
> as well  
> [Parked at Loopia](http://beast.caboo.se/forums/2/topics/870)
> 
> Image.transaction do  
> images = params[:image].collect { |img| Image.create(:uploaded\_data =\>  
> img }  
> end unless params[:image].nil?
> 
> basically i think i can have a form with a dynamic amount of file fields  
> for each file i would like to upload. Image i guess is the table that  
> will be storing the photos.
> 
> i am confused at the block part. basically is this converting the hash  
> into an array, going through each index in the array, and saving each  
> photo into the Image table?

Collect takes every element in the array and replaces it with the result  
of the passed block. The unless after the transaction block prevent the  
block from executing if the condition is true.

So you could rewrite the code like this:

unless params[:image].nil?  
Image.transaction do  
images = []  
params[:image].each do |img|  
images \<\< Image.create(:uploaded\_data =\> img)  
end  
end  
end

But what fun is that? Although, personally I don’t like a condition  
after a multi-line block like that. You don’t realize it will be  
conditionally executed until the end of it. But it’s a great case for  
the use of collect.

---

<div class="post-metadata">

**Author:** ![treebeard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@treebeard](https://www.ruby-forum.com/u/treebeard)\
**Post date:** [March 14, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/can-someone-explain-this-code-to-me/88824/3 "2007-03-14T19:59:28Z")

</div>

hi Alex, thanks for the post. however, im still trying to understand  
this…

images = []  
params[:image].each do |img|  
images \<\< Image.create(:uploaded\_data =\> img)  
end

params[:image]…the actual file is here correct?

params[:image][:uploaded\_data]  
and is that the same as this?

params[‘0’][:uploaded\_data]  
params[‘1’][:uploaded\_data]

etc… for multiple files?

also, what is the purpose for images? cant one just call

Image.create(:uploaded\_data =\> img) inside the block?

thanks.

Alex W. wrote:

> treebeard wrote:
> 
> > taken from here using acts\_as\_fu, possible work for acts as attachment  
> > as well  
> > [Parked at Loopia](http://beast.caboo.se/forums/2/topics/870)
> > 
> > Image.transaction do  
> > images = params[:image].collect { |img| Image.create(:uploaded\_data =\>  
> > img }  
> > end unless params[:image].nil?
> > 
> > basically i think i can have a form with a dynamic amount of file fields  
> > for each file i would like to upload. Image i guess is the table that  
> > will be storing the photos.
> > 
> > i am confused at the block part. basically is this converting the hash  
> > into an array, going through each index in the array, and saving each  
> > photo into the Image table?
> 
> Collect takes every element in the array and replaces it with the result  
> of the passed block. The unless after the transaction block prevent the  
> block from executing if the condition is true.
> 
> So you could rewrite the code like this:
> 
> unless params[:image].nil?  
> Image.transaction do  
> images = []  
> params[:image].each do |img|  
> images \<\< Image.create(:uploaded\_data =\> img)  
> end  
> end  
> end
> 
> But what fun is that? Although, personally I don’t like a condition  
> after a multi-line block like that. You don’t realize it will be  
> conditionally executed until the end of it. But it’s a great case for  
> the use of collect.

---

<div class="post-metadata">

**Author:** ![treebeard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@treebeard](https://www.ruby-forum.com/u/treebeard)\
**Post date:** [March 14, 2007, 8:05pm UTC](https://www.ruby-forum.com/t/can-someone-explain-this-code-to-me/88824/4 "2007-03-14T20:05:59Z")

</div>

> images=params[:image].collect{|img|Image.create(:uploaded\_data=\>img}
> 
> i am confused at the block part. basically is this converting the hash  
> into an array, going through each index in the array, and saving each  
> photo into the Image table?

Assuming Image is a plain old ActiveRecord class (no overwritten  
methods) then yes. The collect would iterate over every image saving the  
records (the records would contain just two fields with values: id and  
uploaded\_data) and returning finally an array with the AR objects  
created.

regards

javier ramirez

## –

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!

---

<div class="post-metadata">

**Author:** ![treebeard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@treebeard](https://www.ruby-forum.com/u/treebeard)\
**Post date:** [March 14, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/can-someone-explain-this-code-to-me/88824/5 "2007-03-14T20:27:03Z")

</div>

> params[:image]…the actual file is here correct?

If you look at the form, it’s probably using a group of file fields  
with a name of ‘image[]’. Get params have no concept of arrays, so  
this is basically posted like this:

image[]=FILE1&image[]=FILE2

Rails parses fields with names ending in [] into an array. So  
params[:image] is an array of the images from the file field.

> params[:image][:uploaded\_data]  
> and is that the same as this?

Nope.

> params[‘0’][:uploaded\_data]  
> params[‘1’][:uploaded\_data]
> 
> etc… for multiple files?

It’s an array… params[:image][0]

> also, what is the purpose for images? cant one just call
> 
> Image.create(:uploaded\_data =\> img) inside the block?

Yup. Alex was just showing an example using #each instead of  
#collect. #collect will return an array of the created images. With  
#each, you have to populate an images array manually.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![treebeard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@treebeard](https://www.ruby-forum.com/u/treebeard)\
**Post date:** [March 15, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/can-someone-explain-this-code-to-me/88824/6 "2007-03-15T16:28:18Z")

</div>

thanks all and thanks rick for the code you posted over in beast!

---

<div class="post-metadata">

**Author:** ![treebeard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@treebeard](https://www.ruby-forum.com/u/treebeard)\
**Post date:** [March 14, 2007, 9:30pm UTC](https://www.ruby-forum.com/t/can-someone-explain-this-code-to-me/88824/7 "2007-03-14T21:30:13Z")

</div>

On Mar 14, 2007, at 7:52 PM, Alex W. wrote:

> Collect takes every element in the array and replaces it with the  
> result  
> of the passed block.

Just for the record, collect does not modify the array in place, in  
that sense no element is replaced. (Replacement is provided by collect!)

– fxn
