views:

39

answers:

0

I have a Ruby on Rails application that I'm working on an API for an associated iPhone application.

I have overwritten the to_json method of my Item model to return some custom information from the API request that I'll need in the iPhone app.

However, when I use the built-in rails to_json method and include my Item model as an associated parameter, my Item#to_json method is not called, and just the default to_json on the AR object is called.

See the following code (@api_user is a User model object)

    render :json => @api_user.to_json( :include => { :friends => { :include => :items, :except => :api_token } } )

I'm including the friends association which is in turn including the items assocation, but the default to_json on Item is being called, not my custom to_json.

What am I missing?

Here is my Item#to_json method

  def to_json(options={})
{
  :id => self.id,
  :name => name,
  :description => description,
  :url => url,
  :quantity => quantity,
  :price_range_raw => price_range,
  :price_range => human_readable_price_range( price_range ),
  :can_be_purchased => can_be_purchased?,
  :completely_purchased => completely_purchased?,
  :remaining_qty => remaining_quantity,
  :thumb_photo_url => photo.url(:thumb),
  :small_photo_url => photo.url(:small),
  :large_photo_url => photo.url(:large),
  :priority_raw => priority,
  :priority => human_readable_priority( priority )
}.to_json

end

No other models have the #to_json method overriden