You shouldn't need to perform an assets.build for upload more than one photo.
The best way to set up an asset library for your application is to start by specifying a bespoke model for your assets, i.e. attachments. You can then utilise the polymorphic ActiveRecord association (http://railscasts.com/episodes/154-polymorphic-association) to share the Attachment model across multiple models. I would also recommend using Carrierwave as your choice of file management.
Here, in this example, I have set up a polymorphic association called 'attachable':
attachment.rb
def Attachment << ActiveRecord::Base
attr_accessible :attachable_id, :attachable_type, :description, :file
belongs_to :attachable, polymorphic: true
mount_uploader :file, AssetUploader
end
I have also assigned two new attributes to the attachment model which will contain the associated model ID (in your case the prediction ID) and the attached type. Also, notice I have assigned the mount_uploader from Carrierwave to the attachment table (see carrierwave documentation for more information on this).
prediction.rb
def Prediction << ActiveRecord::Base
attr_accessible :title, :description, :attachments_attributes
has_many :attachments, as: :attachable, :dependent => :destroy
accepts_nested_attributes :attachments
end
Now, within my Prediction model, I have created a has_many relation called attachments, which references the attachable polymorphic association in the Attachment table. Furthermore, the associated attachments are dependent on the prediction and will be removed if the prediction is removed.
I have also set up the Attachment model to be editable within the Prediction model. Therefore, you can assign nested fields within the Prediction forms.
Our final step is to configure the Carrierwave uploader file. I'm using MiniMagick Gem to compress and resize the images into different versions, while utilising the 'from_version' method in order to improve processing speed. See below:
app/uploaders/asset_uploader.rb
class AssetUploader < CarrierWave::Uploader::Base
include CarrierWave::Compatibility::Paperclip
include CarrierWave::MiniMagick
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
process resize_to_fit: [500,500]
version :large do
process resize_to_fill: [400,400]
end
version :medium, :from_version => :large do
process resize_to_fill: [150,150]
end
version :small, :from_version => :medium do
process resize_to_fill: [50,50]
end
end
This should be enough to get you started.
UPDATE FOR COMMENTS
My previous comment was to demonstrate best practise for handling the upload of assets, as it allows for scalability later on.
As for resizing images, I have always used MiniMagick as it has always been quick and easy to set up with very few problems. I haven't had much luck with RMagick in the past. Is there a specific reason you want to use RMagick?
UPDATE FOR COMMENTS #2
You can set the list of allowed file types inside your asset_uploader.rb
:
def extension_white_list
%w(jpg jpeg gif png)
end