views:

119

answers:

1

Hello, I'm trying to convert an ActiveRecord model to JSON in Rails, and while the to_json method generally works, the model's virtual attributes are not included. Is there a way within Rails to list not just the attributes of a model, but also it's attr_accessor and attr_reader attributes in order that all readable attributes are available when the model is converted to JSON?

+1  A: 

Use the :method option:

@model.to_json(:method => %w(some_virtual_attribute another_virtual_attribute))
François Beausoleil