This project is archived and is in readonly mode.

#5400 new
Rasmus Rønn Nielsen

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

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>

Pages