ruby on rails 3 - additional attributes in both JSON and XML -



ruby on rails 3 - additional attributes in both JSON and XML -

i trying incorporate attributes in api of rails app. utilize case simple. have user model:

class user < activerecord::base attr_accessible :email end

i have model, linking users event:

class userevent < activerecord::base belongs_to :user belongs_to :event end

i want able list users related event using userevent model through api accessible json or xml, , email of userevent appear in both xml , json dump.

this question suggests can override serialiable_hash, appears work json, looks serializable_hash not used to_xml

another approach have investigated override attributes method in class:

class userevent < activerecord::base def attributes @attributes = @attributes.merge "email" => self.email @attributes end end

this works json, throws error when trying xml version:

undefined method `xmlschema' "2011-07-12 07:20:50.834587":string

this string turns out "created_at" attribute of object. looks doing wrong on hash manipulating here.

you can add together additional nested info api responses using include. here's example:

respond_with(@user, :include => :user_event )

you should add together reverse association in user:

has_many :user_events

you can pass in array :include multiple models. it'll serialize , nest them in response appropriately.

ruby-on-rails-3 activerecord xml-serialization jsonserializer

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -