Rais 3, nested attributes problem with paperclip

I have two models, one model girl and one girl_photos (a file upload
asset). The only code I have in models which describes the association
is:

For the girl => belongs_to :girl and has_attached_file :photo
For the model girl_photos => has_many :girl_photos and
accepts_nested_attributes_for :girl_photos

I’ve tried to save as a nested attribute the photo, it saves the
association in the database but it doesn’t set the name of the image
and doesn’t upload it.

I thought that it might be a problem with paperclip so I created
another test model with no association, just one image upload (in the
same model) and it worked fine (it uploaded the images and saved it
correctly).

In the log file I see the following:
INSERT INTO girls (name, surname, height, weight,
description, created_at, updated_at) VALUES (‘test’, ‘test’,
‘test’, ‘test’, ‘test’, ‘2011-05-27 18:31:57’, ‘2011-05-27 18:31:57’)

INSERT INTO girl_photos (created_at, updated_at,
photo_file_name, photo_content_type, photo_file_size,
photo_updated_at, girl_id) VALUES (‘2011-05-27 18:31:57’,
‘2011-05-27 18:31:57’, NULL, NULL, NULL, NULL, 4)
[paperclip] Saving attachments.

For some reason, on the second query, it saves the photo but it
doesn’t captures the name and it doesn’t upload it. I don’t know what
else could I try or test.

Thank you for your help.

On Fri, May 27, 2011 at 7:23 PM, Yiannis [email protected] wrote:

I have two models, one model girl and one girl_photos (a file upload
asset). The only code I have in models which describes the association
is:

For the girl => belongs_to :girl and has_attached_file :photo
For the model girl_photos => has_many :girl_photos and
accepts_nested_attributes_for :girl_photos

First, the above does not make sense… do you mean the following:

Model Girl
has_many :girl_photos

Model GirlPhotos
belongs_to :girl

I am assuming that your GirlPhoto model has a girl_id field, correct?

Anyhow confirm this as if the above is as you say, the associations look
wrong.

Yes! Sorry David I meant exactly what you wrote and yes my GirlPhoto
model has a girl_id. Sorry for the wrong information.

I am also using paperclip 2.3.8 and rails 3.0.6. I can’t figure out
what is wrong, I checked on google and I found a tutorial which is
exactly what I have done (the tutorial =>
http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/).

It worked. I tried many things and now it uploads my images. Thank you
even though I didn’t understand what was wrong.

2011/5/27 Yiannis [email protected]

Yes! Sorry David I meant exactly what you wrote and yes my GirlPhoto
model has a girl_id. Sorry for the wrong information.

I am also using paperclip 2.3.8 and rails 3.0.6. I can’t figure out
what is wrong, I checked on google and I found a tutorial which is
exactly what I have done (the tutorial =>

http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/

).

I dont know the exact answer, but I would start with a debugger
breakpoint
in the controller create action and see what params you are getting, how
they are getting assigned to your model attributes (I guess you are
probably
using update_attributes), and this should at least give you more
information
on what’s going on.

If still having trouble would not hurt to post your schema for pertinent
models and the model code pertaining to paperclip to see if there is
anything not matching up.

Hi everybody !

This topic is quite old, but since i was confronted to a similar
problem, i
thought it could help anybody interested in.

After consulting the rails API over nested attributes:

, I solved the problem through allowing parameters in the controller.

Adapted to the given example, it should go like that :

File : app/controllers/girls_controller.rb
[…]
def girl_params

params.require(:girl).permit(:name,:surname,:height,:weight,:description,girl_photos_attributes:[:photo,:id,:_destroy])
end

Happy Hacking !

Le samedi 28 mai 2011 03:34:18 UTC+2, Yiannis a écrit :