# Strange behavior with erb template and iteration

**URL:** <https://www.ruby-forum.com/t/strange-behavior-with-erb-template-and-iteration/127530>\
**Category:** Rails\
**Created:** [January 30, 2008, 2:40am UTC](https://www.ruby-forum.com/t/strange-behavior-with-erb-template-and-iteration/127530 "2008-01-30T02:40:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom\_302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e480ec/32.png) [@tom\_302](https://www.ruby-forum.com/u/tom_302)\
**Post date:** [January 30, 2008, 2:40am UTC](https://www.ruby-forum.com/t/strange-behavior-with-erb-template-and-iteration/127530/1 "2008-01-30T02:40:09Z")

</div>

Hello,

I’ve noticed a strange problem in my custom generator template. At  
the top of the template, I iterate through the attribute arguments to  
extract information and construct a Hash. Later in the template, I  
loop over each\_pair of the Hash. The loop executes (I see my DEBUG  
statement) but the block between the loop never makes it from the  
template into the output file.

The problem is eliminated if I don’t re-define a.name inside my  
attribute iterator.

\<%  
table = Hash.new

attributes.each do |a|  
prefix = get\_assoc\_prefix(a.name) #real attribute name  
suffix = get\_assoc\_suffix(a.name) #attribute metadata  
table[prefix] = suffix  
a.name = prefix #THIS LINE PREVENTS THE ‘if’-block FROM BEING  
ADDED TO THE OUTPUT FILE  
end  
%\>

class \<%= class\_name %\> \< ActiveRecord::Base  
def precedence  
sum = 0  
\<% ranks\_by\_attribute.each\_pair do |att, rank|  
puts “DEBUG: att=#{att}, rank=#{rank}” #THIS ALWAYS PRINTS %\>  
if self.\<%= att %\>  
sum += \<%= rank.to\_i %\>  
end  
\<% end %\>  
return sum  
end  
end

ruby 1.8.6 (2007-09-23 patchlevel 110) [i686-darwin9.1.0]  
rails 1.2.5

Am I doing something wrong, or is this a bug?

Thanks,  
Tom

---

<div class="post-metadata">

**Author:** ![tom\_302](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e480ec/32.png) [@tom\_302](https://www.ruby-forum.com/u/tom_302)\
**Post date:** [January 30, 2008, 2:52am UTC](https://www.ruby-forum.com/t/strange-behavior-with-erb-template-and-iteration/127530/2 "2008-01-30T02:52:27Z")

</div>

Opps, bad copy/paste in my simplified case. “table” or  
“ranks\_by\_attribute” are the same hash.
