On Wed, Jan 25, 2012 at 4:55 PM, Peter Vandenabeele
<[email protected]>wrote:

> On Wed, Jan 25, 2012 at 4:03 PM, Jacob H. <[email protected]> wrote:
>
>> Peter Vandenabeele wrote in post #1042445:
>> > On Wed, Jan 25, 2012 at 7:35 AM, Jacob H. <[email protected]> wrote:
>> >
>> >>
>> >>  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
>>
>> Thanks for the reply and the links.  I am running on 3.1.3 which may be
>> the problem but after walking through the commit you sent I believe
>> 3.2.0 is handling it the same way.
>
>
Could it be that is actually solved in Rails 3.2.0 ?
Did you effectively test with 3.2.0?

I just tested now in Rails 3.2.0 and found this:

1.9.3-p0 :012 > p.to_xml(:only => :name, :include => :child)
 => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<parent>\n
<name>dad</name>\n  <child>\n    <created-at
type=\"datetime\">2012-01-24T10:05:43Z</created-at>\n    <id
type=\"integer\">1</id>\n    <name>Sarah</name>\n    <parent-id
type=\"integer\">1</parent-id>\n    <updated-at
type=\"datetime\">2012-01-24T10:05:43Z</updated-at>\n
</child>\n</parent>\n"

This seems to indicate that for the child, all attributes are shown,
independent of the :only on the parent.

There is recent discussion about this on
https://github.com/rails/rails/issues/714




> My argument is that
>> the options should only be passed to the parent model (books) but not
>> the included association (chapters).  Let's expand upon the previous
>> example and say we added one more attribute to the only option.
>>
>> book.to_json(:only => [:id, :name, :price], :include => :chapters)
>>
>> { "id": 1, "name":"The Ruby Way", "price":2000, "chapters":[ { "id" : 1,
>> "name":"serialization" } ] }
>>
>> In the example above, the "price" attribute was added as an :only
>> option.  Since book has a price the value is included in the json.  The
>> problem is chapters doesn't have a price but the option is still passed
>> in during serialization anyway.  Why not start clean for each included
>> relationship?  Instead of passing the same options along, exclude the
>> top level options for :only, :except, and :methods when serializing
>> included associations.
>>
>> What are your thoughts
>>
>
> I agree (the current behavior seems a bug to me). I did not test this
> specific behavior in Rails 3.2.0
>

I have the impression now that the issue is resolved in 3.2.0.

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