Can someone explain this code to me?

taken from here using acts_as_fu, possible work for acts as attachment
as well
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?

treebeard wrote:

taken from here using acts_as_fu, possible work for acts as attachment
as well
Parked at Loopia

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.

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

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.

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 !!Es gratis!!

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://weblog.techno-weenie.net
http://mephistoblog.com

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

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