Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Calculations in JSON
4 Posts
Calculations in JSON

Thank you all. I went through the documentation page. Looks like :methods is the solution I was looking for.

Read the documentation for #to_json. This should take you about a minute, and the answer should be obvious. Here's a link:


You have to deal with ActiveSupport::JSON.encode operation. One way could be:

h = model.attributes h[:total_amount] = model.total_amount json_model = ActiveSupport::JSON.encode(h)

I have a method "total_amount" in the Model which returns the sum(some field from has_many relationship)

I want this value to be present in the json for this Model, obtained by using to_json.

How can I do this?


4 Posts
Login to add your message