On Wed, Jan 25, 2012 at 7:35 AM, Jacob H. <[email protected]> wrote:

> 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.
>

The code in active_model/serialization.rb was changed significantly
between 3.1.3 and 3.2.0. On which version are you working? In
my case, the code in 3.2.0 did the right thing :-)

:only => [] resulted (correctly) in an empty list in 3.2.0, while in 3.1.3
it resulted in all attributes included.

These 2 commits seem to be most relevant:

https://github.com/rails/rails/commit/5b2eb64c
https://github.com/rails/rails/commit/2a663dcf

HTH,

Peter

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to