Hi all ! I''ve written a Character model, each character has many Attributes. My schema.rb looks like this: create_table "characters", :force => true do |t| t.string "first_name" t.string "last_name" t.integer "user_id" t.datetime "created_at" t.datetime "updated_at" end create_table "attributes", :force => true do |t| t.string "name" t.float "value" t.integer "character_id" t.datetime "created_at" t.datetime "updated_at" end And the models look like this: class Character < ActiveRecord::Base has_many :attributes ... end class Attribute < ActiveRecord::Base belongs_to :character end When I want to create a new instance by calling c = Character.new :first_name => "hello", :last_name => "world" I get this Error: ActiveRecord::AssociationTypeMismatch: Attribute(#17724630) expected, got Array(#104100) from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ associations/association_proxy.rb:214:in `raise_on_type_mismatch'' from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ associations/association_collection.rb:229:in `replace'' from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ associations/association_collection.rb:229:in `each'' from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ associations/association_collection.rb:229:in `replace'' from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ associations.rb:1149:in `attributes='' from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/ base.rb:2130:in `initialize'' from (irb):1:in `new'' from (irb):1 from :0 I have no idea why I get this error, since I don''t specify the "attributes" key. I hope you can help me, thank you very much in advance. Greetings, Christoph --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jul-13 19:05 UTC
Re: Problem with ActiveRecord::AssociationTypeMismatch
On 13 Jul 2008, at 19:27, Christoph wrote:> > Hi all ! > I''ve written a Character model, each character has many Attributes. My > schema.rb looks like this: > > create_table "characters", :force => true do |t| > t.string "first_name" > t.string "last_name" > t.integer "user_id" > t.datetime "created_at" > t.datetime "updated_at" > end > > create_table "attributes", :force => true do |t| > t.string "name" > t.float "value" > t.integer "character_id" > t.datetime "created_at" > t.datetime "updated_at" > end > > And the models look like this: > class Character < ActiveRecord::Base > has_many :attributesDon''t do this. You''ll overwrite the existing attributes and attributes= methods. Bad stuff will happen. Fred>--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Oh my god, I didn''t know that "attributes" has a special meaning. Thank you very much, Christoph On 13 Jul., 21:05, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 13 Jul 2008, at 19:27, Christoph wrote: > > > > > > > Hi all ! > > I''ve written a Character model, each character has many Attributes. My > > schema.rb looks like this: > > > create_table "characters", :force => true do |t| > > t.string "first_name" > > t.string "last_name" > > t.integer "user_id" > > t.datetime "created_at" > > t.datetime "updated_at" > > end > > > create_table "attributes", :force => true do |t| > > t.string "name" > > t.float "value" > > t.integer "character_id" > > t.datetime "created_at" > > t.datetime "updated_at" > > end > > > And the models look like this: > > class Character < ActiveRecord::Base > > has_many :attributes > > Don''t do this. You''ll overwrite the existing attributes and > attributes= methods. Bad stuff will happen. > > Fred > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Maybe Matching Threads
- serialized attribute converting to string on reload
- acts_as_enumerated
- Nooby Stuck - "has_and_belongs_to_many" relationship
- design recommendations for authenticating users with lots of different attributes..?
- Site Navigation With Polymorphic Has Many Through