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: https://github.com/gpad/rms
can anybody help?
thanks
Gianluca
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On Nov 24, 5:10 am, Gianluca Padovani <gpadov...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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: > > GEThttp://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: > > PUThttp://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"}Looks like you need a slightly different format for the XML: http://stackoverflow.com/questions/4698266/creating-multiple-resources-in-a-single-restful-post-in-rails --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On Thu, Nov 24, 2011 at 3:40 PM, Gianluca Padovani <gpadovani-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> 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: https://github.com/gpad/rms > > can anybody help? > > thanks > Gianluca > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sergio Angeletti - www.primevoices.com
2011-Nov-26 04:40 UTC
Re: ActiveRecord::AssociationTypeMismatch
Gracias el de una semana es para la semana del 4 al 10. De diciembre SERGIO On Nov 24, 2011 7:24 AM, "Gianluca Padovani" <gpadovani-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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: https://github.com/gpad/rms > > can anybody help? > > thanks > Gianluca > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
2011/11/26 Matt Jones <al2o3cr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> Looks like you need a slightly different format for the XML: > > http://stackoverflow.com/questions/4698266/creating-multiple-resources-in-a-single-restful-post-in-railsI 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Apparently Analagous Threads
- Rails, respond_to? over anonymous module (extend has_many).
- AssociationTypeMismatch with RSpec 1.0.8, autotest running with drb
- Validation problems: ActiveRecord::AssociationTypeMismatch
- Validation problem with active record: AssociationTypeMismatch
- AssociationTypeMismatch / Model Caching in development envirnment