ActiveRecord::AssociationTypeMismatch

Hi to all, I have this error and I don't understand why. I have three model, Image and Playlist and PlaylistItem. Everything works fine. The app should work also a XML REST service. When I made this call I obtain this XML because the playlist don't contains images:

GET http://0.0.0.0:3000/playlists/7.xml <playlist>   <id>7</id>   <name>metallo</name>   <images></images> </playlist>

I would put some images in playlist (the images are already presents on server) I should made this call:

PUT http://0.0.0.0:3000/playlists/7.xml

<playlist>     <id>7</id>     <name>metal</name>     <images>         <image>             <id>1</id>             <name>gino</name>         </image>         <image>             <id>2</id>             <name>pino</name>         </image>     </images> </playlist>

but I obtain this error:

ActiveRecord::AssociationTypeMismatch in PlaylistsController#update Image(#87559180) expected, got Array(#78093260)

parameters: {"playlist"=>{"id"=>"7", "name"=>"metal", "images"=>{"image"=>[{"id"=>"1", "name"=>"gino"}, {"id"=>"2", "name"=>"pino"}]}}, "id"=>"7", "format"=>"xml"}

This are the three models:

class Image < ActiveRecord::Base   attr_accessor :picture   validates :name, :uniqueness => true, :presence => true   validates :picture, :presence => true

  has_many :playlist_items   has_many :playlists, :through => :playlist_items end

class Playlist < ActiveRecord::Base   validates :name, :presence => true, :uniqueness => true

  has_many :playlist_items   has_many :images, :through => :playlist_items   accepts_nested_attributes_for :images, :allow_destroy => true end

class PlaylistItem < ActiveRecord::Base   belongs_to :image   belongs_to :playlist end

The project can be found here: GitHub - gpad/rms: Exmample REST server with file upload

can anybody help?

thanks Gianluca

Looks like you need a slightly different format for the XML:

--Matt Jones

I got also faced this error . when I was using same column name in two different tables and trying to access them.I renamed the column in one of the tables and it started working.

–RorUser

Gracias el de una semana es para la semana del 4 al 10. De diciembre

SERGIO

I try this type of XML

<playlist>   <id>7</id>   <name>metal</name>   <images_attributes type="array">     <image>       <id>3</id>     </image>   </images_attributes> </playlist>

but I receive this error:

Couldn't find Image with ID=3 for Playlist with ID=7

app/controllers/playlists_controller.rb:43:in `block in update' app/controllers/playlists_controller.rb:42:in `update'

"playlist"=>{"id"=>"7", "name"=>"metal", "images_attributes"=>[{"id"=>"3"}]}, "id"=>"7", "format"=>"xml"}

The image ID 3 already exist!

It's very strange ... I don't think I'm in a case corner, It's common in REST service have this type of information ...

bye Gianluca

Hi to all, I think I have understand the problem. When you made a PUT of this type:

<playlist> <id>7</id> <name>metal</name> <images_attributes type="array">    <image>      <id>3</id>    </image> </images_attributes> </playlist>

Rails suppose that:

Or your are creating the playlist and the image or you are updating the relationship inside the playlist, see the method assign_nested_attributes_for_collection_association in activerecord-3.0.10/lib/active_record/associations/has_many_through_association.rb

In may case I have already all the image in the play list and I would create a playlist selecting some images ...

How can I obtain this behavior?

thanks Gianluca