timpone
timpone

Reputation: 19969

possible to nest method calls on to_json

I'm trying to do something like this:

render :json => r.to_json(:methods => [:food_item => {:method => :price_value}]) 

but it's not working. Is something like this even possible?

thx

edit 1 no association

def food_item
  MenuItem.find(food_id)
end

Upvotes: 0

Views: 60

Answers (1)

xyz
xyz

Reputation: 1497

Is food_item an ActiveRecord association? If so, you could try

render :json => r.to_json(:include => { :food_item => { :only => :price_value } })

I'll refine my answer in response to "edit 1". First, remove your food_item method and add an actual association like this:

belongs_to :food_item, :class_name => "MenuItem", :foreign_key => "food_id"

and then do

render :json => r.to_json(:include => { :food_item => { :only => [:price_value] } })

Upvotes: 1

Related Questions