2012-03-13 55 views
1

我呈現出一些JSON是這樣的:負載關聯的對象:方法

render :json => r.to_json(:methods => ['food_item','drink_item']) 

兩個FOOD_ITEM和drink_item有has_one相關價格。我怎麼能加載這個在json中呈現?

THX

編輯#1 這裏的一些代碼 - 寫昨天深夜:

class MenuItem < ActiveRecord::Base 
    ... 
    #price 
    has_one :price, :as => :pricable 
    accepts_nested_attributes_for :price 
end 

class ObjectConnection < ActiveRecord::Base 
    ... 
    def food_item 
    MenuItem.find(food_id) 
    end 

    def drink_item 
    MenuItem.find(drink_id) 
    end 
end 
+0

你能否發佈你的模型代碼? – 2012-03-13 08:36:58

回答

2

在此,你需要在你的方法food_itemdrink_item

def food_item 
    food_item.to_json(:include => :my_has_one) 
end 

def drink_item 
    drink_item.to_json(:include => :my_has_one) 
end 
使用 :include ARGS
+0

我無法只在json的渲染而不是在模型層指定? – timpone 2012-03-13 16:20:48