當我使用:include子句爲一個多態關聯呈現XML時,它不起作用。我結束了與XML返回對象的指針,而不是實際的對象,如:Rails無法將多態關聯呈現給XML?
<posts>
#<Comment:0x102ed1540>#<Comment:0x102ecaa38>#<Comment:0x102ec7fe0>#<Comment:0x102ec3cd8>
</posts>
然而as_json作品!當我渲染JSON有:包括條款,該協會正在正確地呈現和我得到的是這樣的:
posts":[
{"type":"Comment","created_at":"2010-04-20T23:02:30-07:00","id":7,"content":"fourth comment"},
{"type":"Comment","created_at":"2010-04-20T23:02:26-07:00","id":6,"content":"third comment"}]
我目前的解決方法是使用XML的建設者,但我不是太舒服與從長遠來看。有沒有人碰巧知道這個問題?我很喜歡catch-22,因爲雖然XML不會呈現關聯,但as_json不會以猶太人json格式呈現(返回一個數組而不是一個散列列表,因爲正確的json應該)和解串器I' m在客戶端使用將需要修改才能正確解析json。
編輯我使用2.3.5 - 還我使用多態的has_many_polymorphs寶石有很多:通過,可能會引發一個問題...
的模型是,我有去處,並且每個視頻羣聊都有很多可以發表評論,照片等內容的帖子。
XML控制器代碼: format.xml {render:xml => @ hangouts.to_xml(:include =>:users,:methods = >:posts)}
json的代碼類似(模型中): def as_json(options) super(:include =>:users,:methods =>:posts)
您正在使用什麼Rails版本? – 2010-04-24 11:40:31
粘貼您的控制器代碼 – bensie 2010-04-24 19:43:41
jpartogi,bensie:我使用的是2.3.5 - 我也使用has_many_polymorphs gem for polymorphic有很多:through,這可能會導致問題... 模型是我有環聊,並且每個視頻羣聊都有很多可以發表評論,照片等的帖子。 XML控制器代碼: format.xml {render:xml => @hangouts。json的代碼是類似的(模型中): def as_json(options) super(:include =>:users,:methods => to_xml(:include =>:users,:methods =>:posts)} :posts) – ambertch 2010-04-25 07:19:18