Help with loading data with sequential id

Hello,

I’m trying to load data into my mysql table using the following:

============ load_codes_data.rb ============
require ‘active_record/fixtures’
class LoadCodesData < ActiveRecord::Migration
def self.up
down
directory = File.join(File.dirname(“MY_DIR_NAME”), “data”)
Fixtures.create_fixtures(directory, “codes”)
end

def self.down
Code.delete_all
end
end

and here’s the yaml file:

=============== codes.yml ===================
I:
seq: 1
cat: source
description: Member
II:
seq: 2
cat: source
description: Friends or Family
III:
seq: 3
cat: source
description: Book
IV:
seq: 4
cat: source
description: Search Engine
V:
seq: 5
cat: source
description: Magazine
VI:
seq: 6
cat: source
description: Other

Here’s my question.
Codes table has the three fields you see in the yaml file and a unique
id field that gets populated automatically and gets assigned a random
non-sequential numbers. I would like for it to get sequential ids
assigned. I’ve searched for a while, but haven’t come across a
solution… Can anyone help?

Thanks,
Kumi

I suppose you want use the omap YAML type. Read about it in
http://api.rubyonrails.org/classes/Fixtures.html and in
Ordered Mapping Language-Independent Type for YAML™ Version 1.1.

Regards.

Franco C…

Thanks for the pointer! I modified the codes.yml file to look like
this (below) and now it inserts ordered list with sequential ids. I
had to try it several times to get the spacing right… I had two
spaces (no tabs) in front of the key:value pair initially, but it
needed four spaces instead.

=============== codes.yml ===================
— !omap

  • r1:
    id: 1
    seq: 1
    cat: source
    description: Member
  • r2:
    id: 2
    seq: 2
    cat: source
    description: Friends or Family
  • r3:
    id: 3
    seq: 3
    cat: source
    description: Book
  • r4:
    id: 4
    seq: 4
    cat: source
    description: Search Engine
  • r5:
    id: 5
    seq: 5
    cat: source
    description: Magazine
  • r6:
    id: 6
    seq: 6
    cat: source
    description: Other

=========================================