# Error on update\_attributes with HABTM relationship

**URL:** <https://www.ruby-forum.com/t/error-on-update-attributes-with-habtm-relationship/194249>\
**Category:** Rails\
**Created:** [September 1, 2010, 4:06pm UTC](https://www.ruby-forum.com/t/error-on-update-attributes-with-habtm-relationship/194249 "2010-09-01T16:06:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![CUCUCU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4af34b/32.png) [@CUCUCU](https://www.ruby-forum.com/u/CUCUCU)\
**Post date:** [September 1, 2010, 4:06pm UTC](https://www.ruby-forum.com/t/error-on-update-attributes-with-habtm-relationship/194249/1 "2010-09-01T16:06:02Z")

</div>

Hope someone can tell me what’s up, because this has me baffled.

I have an Image model, and a Provider model. Both are defined as  
“has\_and\_belongs\_to\_many” relating to each other. In my image form  
partial, I have a bit of AJAX generating the provider\_ids array  
dynamically from a provider AJAX search on the same page.

So for example, each time I click on a provider name that comes up in  
the search, it adds a line like this to the form markup:

On hitting the submit button, Rails saves the Image model as expected,  
and creates all the entries in my images\_providers table. (consisting  
of just image\_id and provider\_id)

The problem comes in when editing an image. On the edit page, I have  
the same functionality as above available, but the existing provider  
entries are already displayed on the page. So I’ll have in the form:

Now no matter what I do, I cannot get Rails to update the Image on  
submit. I’m using a dead simple method:

def update  
@image = Image.find(params[:id])

```
respond_to do |format|
  if @image.update_attributes(params[:image])
    format.html { redirect_gallery_edit }
  else
    format.html { render :action => "edit" }
  end
end

```

end

However, when this runs, Rails spits out the following:

Provider(#36927920) expected, got Provider(#39471020)

If I update an image with NO linked providers, everything is fine. I’m  
stumped. Here’s the last couple of lines from the trace, in case it’s  
helpful:

activerecord (3.0.0) lib/active\_record/associations/  
association\_proxy.rb:260:in `raise_on_type_mismatch' activerecord (3.0.0) lib/active_record/associations/ association_collection.rb:522:in`block in remove\_records’  
activerecord (3.0.0) lib/active\_record/associations/  
association\_collection.rb:522:in `each' activerecord (3.0.0) lib/active_record/associations/ association_collection.rb:522:in`remove\_records’  
activerecord (3.0.0) lib/active\_record/associations/  
association\_collection.rb:221:in `delete' activerecord (3.0.0) lib/active_record/associations/ association_collection.rb:360:in`block in replace’  
activerecord (3.0.0) lib/active\_record/associations/  
association\_collection.rb:158:in `block in transaction' activerecord (3.0.0) lib/active_record/connection_adapters/abstract/ database_statements.rb:139:in`transaction’  
activerecord (3.0.0) lib/active\_record/transactions.rb:204:in  
`transaction' activerecord (3.0.0) lib/active_record/associations/ association_collection.rb:157:in`transaction’  
activerecord (3.0.0) lib/active\_record/associations/  
association\_collection.rb:359:in `replace' activerecord (3.0.0) lib/active_record/associations.rb:1524:in`block  
in collection\_accessor\_methods’  
activerecord (3.0.0) lib/active\_record/associations.rb:1532:in `block in collection_accessor_methods' activerecord (3.0.0) lib/active_record/base.rb:1548:in`block in  
attributes=’  
activerecord (3.0.0) lib/active\_record/base.rb:1544:in `each' activerecord (3.0.0) lib/active_record/base.rb:1544:in`attributes=’  
activerecord (3.0.0) lib/active\_record/persistence.rb:127:in `block in update_attributes' activerecord (3.0.0) lib/active_record/transactions.rb:289:in`block  
in with\_transaction\_returning\_status’  
activerecord (3.0.0) lib/active\_record/connection\_adapters/abstract/  
database\_statements.rb:139:in `transaction' activerecord (3.0.0) lib/active_record/transactions.rb:204:in`transaction’  
activerecord (3.0.0) lib/active\_record/transactions.rb:287:in  
`with_transaction_returning_status' activerecord (3.0.0) lib/active_record/persistence.rb:126:in`update\_attributes’
