Undefined method `serializable_hash' for array
I am using mongoid as my orm, and i call to_json on the result set. The initial implementation works fine, except for the fact that everything is loaded on the fly (as opposed to eager loading)
Simplified schema and code below:
Answer has_many likers
all_answers=Answer.includes(:likers).all.map{|a| a}
all_answer.to_json(:only=>[:text],:include => {:likers=>{:only=>[:_id,:nickname]}})
I started using thi开发者_运维技巧s great gem (mongoid_eager_loading), and it works great. However, when i call to_json on the result set
all_answers=Answer.includes(:user, :question,:likers,:comments).all.map{|a| a}
all_answer.to_json(:only=>[:text],:include => {:likers=>{:only=>[:_id,:nickname]}})
I will get this error:
undefined method `serializable_hash' for #<Array:0x00000105b3d020>
This error only happens when I am trying to include an array for example: Answer has_many likers.
I suspect that arrays contained in individual objects does not have the serializable_hash method.
How could I resolve this problem? Any ideas would be greatly appreciated!
Take a look at this post from Miso Engineering:
http://engineering.gomiso.com/2011/05/16/if-youre-using-to_json-youre-doing-it-wrong/
The RABL gem (https://github.com/nesquena/rabl) is worth a look if you're pushing your models out as JSON.
精彩评论