Take a look at these:
http://blog.codefront.net/2007/07/11/better-json-output-from-rails-with-the-jsonifier-plugin/
http://dev.rubyonrails.org/ticket/9677
Basically, you need an :include
Arshak
Infinit wrote:> Hi all,
>
> I hv the following model:
>
> class User < ActiveRecord::Base
> has_and_belongs_to_many :roles
>
> def user_role
> self.roles
> end
> end
>
> class Role < ActiveRecord::Base
> has_and_belongs_to_many :users
> end
>
> When I run the following code:
> user = User.find(:first)
> user.to_json
>
> there''s no user_role attribute displayed. but when I run
> user.user_role, it can give the expected result.
> So, how can I get the associated attribute when using to_json ?
>
> Appreciate for your help.
--
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-/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
-~----------~----~----~----~------~----~------~--~---