Heinz Strunk
2011-Oct-05 15:00 UTC
undefined method keys for ActiveRecord::Relation when executing to_json
Hello, I just ran into a strange error. In console I typed in following: ruby-1.9.2-p290 :120 > User.first.to_json User Load (0.7ms) SELECT `users`.* FROM `users` LIMIT 1 => "{\"avatar_id\":10,\"email\":\"peter-hcDgGtZH8xNAfugRpC6u6w@public.gmane.org\",\"firstname\":\"Peter\",\"lastname\":\"L\",\"user_role_id\":11}" and when I try that with an avatar: ruby-1.9.2-p290 :121 > Avatar.first.to_json Avatar Load (0.5ms) SELECT `avatars`.* FROM `avatars` LIMIT 1 NoMethodError: Attribute Load (0.8ms) SELECT `attributes`.* FROM `attributes` WHERE `attributes`.`avatar_id` = 10 undefined method `keys'' for #<ActiveRecord::Relation:0x007fb6a3e69518> Why on earth can I convert the user model to json but not the avatar?! Any suggestions appreciated! Hans -- Posted via http://www.ruby-forum.com/. -- 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.
Colin Law
2011-Oct-05 15:14 UTC
Re: undefined method keys for ActiveRecord::Relation when executing to_json
On 5 October 2011 16:00, Heinz Strunk <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello, > > I just ran into a strange error. In console I typed in following: > ruby-1.9.2-p290 :120 > User.first.to_json > User Load (0.7ms) SELECT `users`.* FROM `users` LIMIT 1 > => > "{\"avatar_id\":10,\"email\":\"peter-hcDgGtZH8xNAfugRpC6u6w@public.gmane.org\",\"firstname\":\"Peter\",\"lastname\":\"L\",\"user_role_id\":11}" > > and when I try that with an avatar: > ruby-1.9.2-p290 :121 > Avatar.first.to_json > Avatar Load (0.5ms) SELECT `avatars`.* FROM `avatars` LIMIT 1 > NoMethodError: Attribute Load (0.8ms) SELECT `attributes`.* FROM > `attributes` WHERE `attributes`.`avatar_id` = 10 > undefined method `keys'' for #<ActiveRecord::Relation:0x007fb6a3e69518>Can you show us the start of avatar.rb and attribute.rb? Also I just wonder whether attribute is a reserved word in RoR. Colin> > Why on earth can I convert the user model to json but not the avatar?! > > Any suggestions appreciated! > Hans > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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. > >-- gplus.to/clanlaw -- 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.
Heinz Strunk
2011-Oct-05 15:31 UTC
Re: undefined method keys for ActiveRecord::Relation when executing to_json
Oh, yeah. You might be right with the reserved word! avatar.rb: class Avatar < ActiveRecord::Base attr_accessible :name, :level, :current_xp, :overall_xp, :gender_cd as_enum :gender, :female => 0, :male => 1 has_one :user has_many :attributes ... attribute.rb: class Attribute < ActiveRecord::Base belongs_to :attribute_type belongs_to :avatar Do I need to rename the whole model now? -- Posted via http://www.ruby-forum.com/. -- 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.
Heinz Strunk
2011-Oct-05 15:32 UTC
Re: undefined method keys for ActiveRecord::Relation when executing to_json
Ehm, yes. Changing it to has_many :avatar_attributes, :class_name => ''Attribute'' did the trick. Thanks a lot for the hint, Colin! -- Posted via http://www.ruby-forum.com/. -- 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.
Colin Law
2011-Oct-05 15:37 UTC
Re: Re: undefined method keys for ActiveRecord::Relation when executing to_json
On 5 October 2011 16:32, Heinz Strunk <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Ehm, yes. > > Changing it to > has_many :avatar_attributes, :class_name => ''Attribute'' > did the trick. > > > Thanks a lot for the hint, Colin!Glad to be of help. Colin> > -- > Posted via http://www.ruby-forum.com/. > > -- > 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. > >-- gplus.to/clanlaw -- 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.