Erb behavior


#1

I’m playing with dynamic test fixtures and seeing what seems to be odd
behavior. I’m fairly new to ruby and rails so it could be something
simple. I have the following fixture:

<%COUNTRY_MAX=10%>
<%0.upto(COUNTRY_MAX) do |number| %>
country<%=number%>:
id: <%=number%>
code: <%=format("%03d", number)%>
name: country<%=format("%03d", number)%>
<% end %>

which produces:
DELETE FROM countries
INSERT INTO countries (“name”, “code”, “id”) VALUES (‘country003’, 3,
3)
INSERT INTO countries (“name”, “code”, “id”) VALUES (‘country004’, 4,
4)
INSERT INTO countries (“name”, “code”, “id”) VALUES (‘country005’, 5,
5)
INSERT INTO countries (“name”, “code”, “id”) VALUES (‘country006’, 6,
6)
INSERT INTO countries (“name”, “code”, “id”) VALUES (‘country007’, 7,
7)
INSERT INTO countries (“name”, “code”, “id”) VALUES (‘country008’,
‘008’, 8)
INSERT INTO countries (“name”, “code”, “id”) VALUES (‘country009’,
‘009’, 9)
INSERT INTO countries (“name”, “code”, “id”) VALUES (‘country000’, 0,
0)
INSERT INTO countries (“name”, “code”, “id”) VALUES (‘country001’, 1,
1)
INSERT INTO countries (“name”, “code”, “id”) VALUES (‘country010’, 8,
10)
INSERT INTO countries (“name”, “code”, “id”) VALUES (‘country002’, 2,
2)

I have several questions about this.

  1. why does the code get out of sync with the name and id?
  2. why aren’t they in order as expected?
  3. why is the code not always formatted to be length 3?

#2

On 7/22/07, dailer removed_email_address@domain.invalid wrote:

I have several questions about this.

  1. why does the code get out of sync with the name and id?
  2. why aren’t they in order as expected?
  3. why is the code not always formatted to be length 3?

Fixtures are just YAML files. What you’re defining is 10 entries in a
Ruby hash. Hashes are unordered. The records should not be expected
to be INSERT’d in order.


Chris W.
http://errfree.com // http://errtheblog.com


#3

Ok, and that led me to YAML:Omap so I can take care of issue #2.

Anyone on #1 and #3?


#4

On 7/23/07, dailer removed_email_address@domain.invalid wrote:

Ok, and that led me to YAML:Omap so I can take care of issue #2.

Jumping in to the thread late…

YAML::Omap is not indicated for database fixtures. Is that what we
were discussing?

if so, shouldn’t each fixture record have an explicit ‘id: 2’, so
their hash order doesn’t matter?

Sorry but I’m pretty good with Omap, but Google won’t show me the rest
of this thread. Jeeze good free help is hard to find!


Phlip
http://www.oreilly.com/catalog/9780596510657/
“Test Driven Ajax (on Rails)”
assert_xpath, assert_javascript, & assert_ajax


#5

thanks.


#6

On 7/22/07, dailer removed_email_address@domain.invalid wrote:

  1. why does the code get out of sync with the name and id?

Because 001 is not a decimal number in Ruby, it’s an octal. You need
to surround your <%= %> with quotes so Ruby knows you want a string
when loading from YAML.

In irb:

010
=> 8

‘010’
=> “010”

  1. why is the code not always formatted to be length 3?

See #1.


Chris W.
http://errfree.com // http://errtheblog.com