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’