0

I have a Profile -> Photo relationship on the "server" application. The client application would the ability to upload up to three photos to the server side.

A side note: I am using Paperclip plugin on the serving app.

I have done something like this successfully with a one-to-one relationship but never with a has_many.

Naturally, ActiveResource does not have the build method.

How can each Photo be initialized?

3.times{@profile.photos.build} 

will not work

How will this be accomplished in the view also? I was considering manually coding 3 file_fields naming each one. e.g.

    <%= file_field_tag 'profile[photos_attributes][0][data]' %>
    <%= file_field_tag 'profile[photos_attributes][1][data]' %>
    <%= file_field_tag 'profile[photos_attributes][2][data]' %>

would I need a fields_for block for each photo instance?

Long story short, how do I successfully initialize/accept the form data for Profile and its child model, Photo?

WYSRD
  • 17
  • 5

1 Answers1

0

You don't need to use a build method, try this:

3.times { @profile.photos << Photo.new }
Adam Lassek
  • 35,156
  • 14
  • 91
  • 107
  • would this require a fields_for block? If so, how would each distinct field be differentiated? Since the auto-magic field generation won't work here, how would the template know to build 3 file_fields? – WYSRD Dec 02 '10 at 01:07
  • @Martin What you might not realize is that `fields_for` is an _iterator_. If there are three empty photo objects, it will create fields for all three. – Adam Lassek Dec 02 '10 at 01:13