2013-12-21 45 views
0

我正在使用Ruby和Sinatra发布Web服务的项目。我遇到的问题是我无法弄清楚如何让数组正确地序列化。我有两条路线。控制数组的JSON序列化

get '/Post' do 
    postId= params[:id] 
    my_post = Post.new(BSON::ObjectId(postId)) 
    return my_post.to_json() #runs the to_json method in post 
end 

get '/SourcePosts' do 
    sourceId = params[:source] 
    my_source = Source.new(BSON::ObjectId(sourceId)) #returns an array of Posts 
    return my_source.get_posts.to_json() #ignores the to_json method in post 
end 

第一Post作品如我所料,打电话给我定制to_json其省略了延迟加载属性的方法。第二个SourcePosts忽略我的覆盖并转储所有内容,包括需要加载数据库倾角的属性。我的问题:如何让ruby调用我的方法,或者在将数组序列化为JSON时忽略昂贵的属性?

+0

不知道我是否正确理解你的问题,但你不应该在代码块中使用'return'关键字,因为它可能破坏'get'方法的逻辑。 – andrykonchin

回答

0

问题在于模型类中存在require 'active_support/all',这是干扰数组序列化的。