Strange behavior with erb template and iteration

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

Opps, bad copy/paste in my simplified case. “table” or
“ranks_by_attribute” are the same hash.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs