I am using Rails 3 and here is my model
class LineItem < ActiveRecord::Base
attr_reader :price
belongs_to :product
def price
self.product.price * self.quantity
end
def as_json(options = {})
super(:include => [:product])
end
end
Above code works. However now I want my json to also have value for price in addition to the other values that I am getting.
How do I accomplish that?
You can use
:methods:You might want to pay proper attention to any incoming
:includeand:methodsettings in youroptionsthough. So you might want to use the block form ofmerge: