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

7
metallo

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

7 metal 1 gino 2 pino

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

On Nov 24, 5:10am, Gianluca P. [email protected] wrote:


{“id”=>“2”,
“name”=>“pino”}]}},
“id”=>“7”,
“format”=>“xml”}

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

–Matt J.

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

2011/11/26 Matt J. [email protected]:

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

I try this type of XML

7 metal 3

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:

7 metal 3

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

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

SERGIO