This project is archived and is in readonly mode.
ActiveRecord::Serialization.serializable_hash uses base :only/:expect for included models
Reported by Rasmus Rønn Nielsen | August 18th, 2010 @ 10:31 AM
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.
Comments and changes to this ticket
-
michael groble January 24th, 2011 @ 01:06 AM
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.
-
Paul Rosania March 2nd, 2011 @ 10:56 PM
+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?
-
Pablo March 29th, 2011 @ 08:15 PM
Hello, we are adding an smaller patch for the JSON Serialization.
An additional ticket may be opened for correcting it in XML.
Regards, -
Pablo March 29th, 2011 @ 08:18 PM
- no changes were found...
-
Federico Brubacher March 30th, 2011 @ 11:23 AM
- Tag set to json activerecord serialization
- Assigned user set to Santiago Pastorino
-
Federico Brubacher April 22nd, 2011 @ 01:28 PM
Santiago, Jeremy have you guys been able to review this patch ?
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>