1

I am rendering out some json like this:

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

Both food_item and drink_item have a has_one associated price. How could I load this to be rendered in the json?

thx

edit #1 Here's some more code - wrote late last night:

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
1
  • Could you please post your model code? Commented Mar 13, 2012 at 8:36

1 Answer 1

2

In this you need use the :include args in your method food_item and 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
Sign up to request clarification or add additional context in comments.

1 Comment

there's no way I can just specify at the rendering of the json rather than at model layer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.