Forum: Ruby on Rails erb behavior

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2a13a8755be0c992152547834a15d3d5?d=identicon&s=25 dailer (Guest)
on 2007-07-23 05:38
(Received via mailing list)
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?
B8dbb1987e8e5318584865f880036796?d=identicon&s=25 Chris Wanstrath (defunkt)
on 2007-07-23 11:29
(Received via mailing list)
On 7/22/07, dailer <d.sailer@comcast.net> 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 Wanstrath
http://errfree.com // http://errtheblog.com
2a13a8755be0c992152547834a15d3d5?d=identicon&s=25 dailer (Guest)
on 2007-07-23 13:38
(Received via mailing list)
Ok, and that led me to YAML:Omap so I can take care of issue #2.

Anyone on #1 and #3?
B8dbb1987e8e5318584865f880036796?d=identicon&s=25 Chris Wanstrath (defunkt)
on 2007-07-23 14:18
(Received via mailing list)
On 7/22/07, dailer <d.sailer@comcast.net> 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"

> 3) why is the code not always formatted to be length 3?

See #1.

--
Chris Wanstrath
http://errfree.com // http://errtheblog.com
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2007-07-23 16:02
(Received via mailing list)
On 7/23/07, dailer <d.sailer@comcast.net> 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
2a13a8755be0c992152547834a15d3d5?d=identicon&s=25 dailer (Guest)
on 2007-07-24 04:43
(Received via mailing list)
thanks.
This topic is locked and can not be replied to.