I got a Ruby model “Comment” with several attributes: username, date_added, modify_date, etc. When I create an instance of this model, call it “i”, and call i.attributes, it gives me all the attributes for this model. Is there anyway to make it not return several attributes I want to keep private.
Reason being, I am lazily calling to_json for i.attributes, and some models contain sensitive information such as password, and I want to make these private.
Try using the :only or :except options for the #to_json method. For example:
… or without hash rockets if you’re on Ruby 1.9 …