I’m trying to write a helper method similar to attr_accessible but
instead it will be used to whitelist which attributes are available for
serialization (attr_serializable). My first attempt at this was to
override serialized_hash and modify the :only option to include nothing
but serializable attributes. I’m facing a problem when nested models
are ‘included’ using the :include option. The :only options for the
parent model are also getting sent to the nested model(s). Here’s an
example:
model1.to_json(:include => model2, :only => [:id])
First serializable_hash gets called for model1 and is passed an options
hash with :only set to an array including :id. This makes sense to me
since the :only option was specified as a top level options. Next
serializable_hash gets called for model2 and it is also passed an
options hash that include :id in the :only option. Why would :only =>
[:id] be an option for the included model (model2)? Same goes for
:methods and :except options set at the top level. I would expect the
:only , :except, and :methods options to be removed if they are not
specified directly on the included model as such:
model1.to_json(:include => {:model2 => {:only =>
[:some_other_property]}, :only => [:id])
Can someone please explain to me why it works this way.