在创建类似于使用to_json
时的方式时,是否可以包含方法或嵌套属性?
使用to_json时,我可以包含published_events方法和一些嵌套方法:
render :json => @venue.to_json(:include => {:published_events => {:methods => :to_param, :include => :occurrences}})
创建类似项目的哈希值时可以做到吗?
@area_attractions = Venue.find(:all, :conditions => ['attraction = ? AND featured = ?', true, true], :limit => 8)
hash = { :main => @main_features, :local_hotspots => @local_hotspots, :area_attractions => @area_attractions }
编辑: 我最终将这个哈希渲染为JSON:
render :json => hash.to_json
答案 0 :(得分:0)
Finder方法返回数组,而不是哈希。试试这个:
hash = { :main => @main_features,
:local_hotspots => @local_hotspots,
:area_attractions => Hash[*@area_attractions] }
答案 1 :(得分:0)
当调用to_json时,散列中的项目仍然可以包含方法
render :json => hash.to_json(:methods => [:image_url, :to_param, :thumb_url])