# Paperclip model fails while when updating with no attachment

**URL:** <https://www.ruby-forum.com/t/paperclip-model-fails-while-when-updating-with-no-attachment/173370>\
**Category:** Rails\
**Created:** [August 7, 2009, 3:11pm UTC](https://www.ruby-forum.com/t/paperclip-model-fails-while-when-updating-with-no-attachment/173370 "2009-08-07T15:11:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [August 7, 2009, 3:11pm UTC](https://www.ruby-forum.com/t/paperclip-model-fails-while-when-updating-with-no-attachment/173370/1 "2009-08-07T15:11:36Z")

</div>

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:

```auto
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:

```auto
<%- 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

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [August 7, 2009, 3:14pm UTC](https://www.ruby-forum.com/t/paperclip-model-fails-while-when-updating-with-no-attachment/173370/2 "2009-08-07T15:14:18Z")

</div>

Ooops… I copied the older file’s create\_or\_update\_assets! method

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

-Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [August 7, 2009, 3:47pm UTC](https://www.ruby-forum.com/t/paperclip-model-fails-while-when-updating-with-no-attachment/173370/3 "2009-08-07T15:47:59Z")

</div>

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
