我正在渲染一些像这样的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 :(得分:2)
在此,您需要使用方法:include
和food_item
drink_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