# Render mutates locals hash?

**URL:** <https://www.ruby-forum.com/t/render-mutates-locals-hash/169886>\
**Category:** Rails\
**Created:** [June 9, 2009, 11:35pm UTC](https://www.ruby-forum.com/t/render-mutates-locals-hash/169886 "2009-06-09T23:35:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [June 9, 2009, 11:35pm UTC](https://www.ruby-forum.com/t/render-mutates-locals-hash/169886/1 "2009-06-09T23:35:58Z")

</div>

If you pass a locals hash into render() … why would Rails mutate that  
hash, adding things to it? This seems awfully odd to me.

Could have something to do with calling partials inside partials, which  
I am doing. Does the locals hash ‘inherit’ to the ‘inner’ partial?  
That’s kind of odd.

I just traced a weird bug in my app down to this – I was storing locals  
in an actual hash data structure, instead of using an anonymous hash,  
and wasn’t counting on rails changing in between calls!

By "freeze"ing the thing that I didn’t expect to get modified, I could  
get this stack trace pointing to what’s doing the modifying, but I  
haven’t looked at it yet.

```
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_view/partial_template.rb:48:in

```

`[]=' /usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action_view/partial_template.rb:48:in`add\_object\_to\_local\_assigns!’  
/usr/lib/ruby/gems/1.8/gems/actionpack-2.1.2/lib/action\_view/partial\_template.rb:9:in  
`initialize’

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [June 10, 2009, 2:50am UTC](https://www.ruby-forum.com/t/render-mutates-locals-hash/169886/2 "2009-06-10T02:50:42Z")

</div>

On Jun 9, 10:35 pm, Jonathan R. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> If you pass a locals hash into render() … why would Rails mutate that  
> hash, adding things to it? This seems awfully odd to me.

Rails has a hash of those local variables that should be created  
inside a partial (for example if you are rendering a collection then  
the current object in that collection is assigned to the appropriate  
local variable name). If you supply a hash it uses that (perhaps it  
should dup the hash you provide). This shouldn’t have anything todo  
with rendering partials from partials

Fred
