0

在创建类似于使用时的哈希时是否可以包含方法或嵌套属性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 
4

2 回答 2

0

Finder 方法返回数组,而不是哈希。尝试这个:

hash = { :main => @main_features, 
         :local_hotspots => @local_hotspots, 
         :area_attractions => Hash[*@area_attractions] }
于 2013-04-23T20:39:39.083 回答
0

当调用 to_json 时,哈希中的项目可以/仍将包含方法

 render :json => hash.to_json(:methods => [:image_url, :to_param, :thumb_url])
于 2013-04-23T20:48:10.963 回答