Paperclip model fails while when updating with no attachment

Hi all,

I have paperclip running in my rails environment. I setup a new model
just for paperclip called Assets so that I could upload multiple files
per item (even though I’m only using 1 attachment per item at the
moment). Everything works except for the following:

Updating an item record with no attachment in the form but having
PREVIOUSLY attached an asset while creating or updating. The only way
that this works is if I create an item with no attachment and update the
same item record with no attachment OR if I created an item record with
no attachment and then update the same item record with an attachment.

Basically - Once an asset has been attached to an item record, updating
with no attachment yields the following error:

"ActiveRecord::RecordInvalid in ItemsController#update

Validation failed: Attachment file name can’t be blank, Attachment file
size can’t be blank"

There are validation errors from the Assets Model for the presence of
:attachment_file_name and :attachment_file_size.

I have the below code in my item model:

after_save create_or_update_assets!

  def create_or_update_assets!
    # return if there are no asset_attributes
    return true if asset_attributes.nil? || asset_attributes.empty?

    # pull the attributes from the instance variable
    asset_attributes.each do |attributes_hash|
      attributes_hash.symbolize_keys!

      # if atts_hash has an id (asset already exists). If there's no id
then this is a new asset.
      asset = attributes_hash[:id].blank? ? assets.build : assets.detect
{ |product| product.id == attributes_hash[:id].to_i }

      asset.attachment = attributes_hash[:attachment] # adds the
attributes to the asset

      asset.save! # saves the asset
    end
  end

my view model looks like this:

<%- for asset in @item.assets do -%>
  <% f.fields_for("asset_attributes[]", asset) do |asset_fields| %>
    <p>
      <%= asset_fields.label :attachment, "File" %><br />
      <%= asset_fields.file_field :attachment, :index => nil %><br />
      <%= asset_fields.hidden_field :id, :index => nil unless(
asset.new_record? ) %>
    </p>
  <%- end -%>
<%- end -%>

I’m positive this has to be a lack of some extra checking in my
create_or_update_assets! method. Any help is GREATLY appreciated. Please
let me know if you need any extra info to help solve this.

Many thanks,
Tony

Ooops… I copied the older file’s create_or_update_assets! method

Just replace “product(s)” with “item(s)”. Sorry about that!

-Tony

Solved!

Replace this…

def create_or_update_assets!
# return if there are no asset_attributes
return true if asset_attributes.nil? || asset_attributes.empty?

# pull the attributes from the instance variable
asset_attributes.each do |attributes_hash|
  attributes_hash.symbolize_keys!
 ....

With…

def create_or_update_assets!
# return if there are no asset_attributes
return true if @asset_attributes.blank?

# pull the attributes from the instance variable
@asset_attributes.each do |attributes_hash|
  attributes_hash.symbolize_keys!
....

Hope it helps someone out there.

-Tony