2013-10-17 65 views
0

即時通訊嘗試添加一些方法在一些包括as_json在rails中,但第二包括與方法不顯示在json上。這是代碼做幾個方法裏面包括

  format.json { render :json => { :projects => @projects.as_json(:include => [ 
      :retail, 
      :property => {:methods => :name}, 
      :user => {:methods => [:name, :address] } ]) } } 

,這是生成的JSON:

{"projects":[{"comment":null,"contractor":1,"created_at":"2013-10-17T16:57:36Z","id":5,"property_id":3,"retail_id":1,"updated_at":"2013-10-17T16:57:36Z","user_id":2,"retail":{"address":null,"created_at":"2013-10-16T22:29:38Z","id":1,"name":"asdasdsa","phone":"6677969224","property_id":null,"social_reason_id":null,"updated_at":"2013-10-17T17:18:09Z","web":"www.boxie.io"},"property":{"city":"Culiacan","colony":"Fracc Los Sauces","coordinates":"(24.7802016, -107.34894730000002)","country_id":1,"created_at":"2013-10-17T00:56:32Z","external_number":null,"id":3,"internal_number":2848,"population":null,"state_id":4,"street":"Alame\u00f1a","updated_at":"2013-10-17T00:56:32Z","zipcode":8028,"name":"Calle Alame\u00f1a, Fracc Los Sauces, 8028, Culiacan, Campeche Mexico"}}]} 

,你可以看到該用戶的信息不會出現在JSON。

謝謝

回答

0

您沒有正確包裝他們,你的第二個和第三個孩子的關聯應該是哈希值。

@projects.as_json(:include => [ 
    :retail, 
    { :property => {:methods => :name } }, 
    { :user => {:methods => [:name, :address] } } 
]