Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord::Serialization.serializable_hash uses base :only/:expect for included models

#5400

User (cols: id, name, age) has_many Group (cols: id, name)

The following results is made via console on rails3 edge/ruby1.9.2-rc2.

User.first.serializable_hash :only => :name
=> {"name"=>"rasmus"}

User.first.serializable_hash :include => :group
=> {"id":1, "name"=>"rasmus", :group=>{"id"=>1, "name"=>"Testgroup"}}

User.first.serializable_hash :only => :name, :include => :group
=> {"name"=>"rasmus", :group=>{}}

I would expect the last call to return {"name"=>"rasmus", :group=>{"id"=>1, "name"=>"Testgroup"}} (because I set up no :only/:except for group). But all attributes of the :group hash is removed because of the "base" :only => :name options is used for associated models also.

For now, I know I can do this, but I really think it should not be necessary.

User.first.serializable_hash :only => :name, :include => { :group => { :only => [:id, :name] } }
=> {"name"=>"rasmus", :group=>{"id"=>1, "name"=>"Testgroup"}}

I think the "base :only/:expect" should only apply to the "base object" (in this case user).

If other people feel this needs to be fixed, I'll make a patch. Please share your opinions on this, thanks.

Reported by Rasmus Rønn Nielsen · August 18th, 2010 @ 10:31 AM

State: new
Milestone: none
Assigned to: Santiago Pastorino Santiago Pastorino
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh
    • Importance changed from to Low

    +1

    August 18th, 2010 @ 11:58 AM

  2. michael groble
    michael groble

    I saw this same issue today and decided to try to track it down. Attached is a fix. Note the tests verify to_xml, to_json, and serializable_hash all have the same behavior of not passing on :only and :except to includes.

    This is the first time I've tried to contribute anything so comments/feedback welcome.

    January 24th, 2011 @ 01:06 AM

  3. John Firebaugh
    John Firebaugh

    +1

    Existing behavior is quite surprising.

    February 22nd, 2011 @ 07:20 AM

  4. Paul Rosania
    Paul Rosania

    +1

    We ran into this today and it blocks us from leveraging any of the built-in JSON serialization mechanisms with nested objects.

    The patch no longer applies cleanly. Can we get this into core if the patch is updated?

    March 2nd, 2011 @ 10:56 PM

  5. Pablo
    Pablo

    Hello, we are adding an smaller patch for the JSON Serialization.
    An additional ticket may be opened for correcting it in XML.
    Regards,

    March 29th, 2011 @ 08:15 PM

  6. Pablo
  7. Federico Brubacher
    Federico Brubacher
    • Tag set to json activerecord serialization
    • Assigned user set to Santiago Pastorino

    March 30th, 2011 @ 11:23 AM

  8. Federico Brubacher
    Federico Brubacher

    Santiago, Jeremy have you guys been able to review this patch ?

    April 22nd, 2011 @ 01:28 PM