Foreign keys in YAML fixtures

Hi all, I’m trying to set up my test fixtures, and I’ve recently added foreign keys to my database. Each “channel” is owned by a user, so I need to include a valid user id with each fixture. I’m not sure how to set this up. My YAML is the following

eBay: id: 1 name: eBay created_at: <%= 5.days.ago.to_s :db %> user_id: <%= User.find_by_login(“quentin”) %> antiqueStore: id: 2 name: antique Store created_at: <%= 5.days.ago.to_s :db %> user_id: <%= User.find_by_login(“quentin”) %>

Nothing is inserted into the user table, so the above obviously won’t work. How can I run my users.yaml file when I run my channels test?

Thanks, Todd

Well, you said you've added the foreign keys *to the database* so you may have issues with the order in which your fixtures are loaded. If you just need to have the entries of a single fixture loaded to the database in a particular order, you can replace the default YAML map with an omap (ordered map) type quite easily:

--- !omap - eBay:    id: 1    name: eBay    created_at: <%= 5.days.ago.to_s :db %>    user_id: 4 - antiqueStore:    id: 2    name: antique Store    created_at: <%= 5.days.ago.to_s :db %>    user_id: 5

But you still (potentially) have the issue of loading the users table before your channels.

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

Hi Stephan, Thanks for the response, I apologize for the delay in my response, I only have time to work on RoR on the weekends. I do have a users file. I have included the text below. As far as ordering, will the ordering plugin be implicitly executed when I run my rake file, or will I have to manually run the target?

Thanks, Todd

quentin: id: 1 login: quentin email: quentin@example.com salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test

#crypted_password: “ce2/iFrNtQ8=\n” # quentin, use only if you’re using 2-way encryption created_at: <%= 5.days.ago.to_s :db %> activated_at: <%= 5.days.ago.to_s :db %> # only if you’re activating new signups

enabled: <%= true %>

aaron: id: 2 login: aaron email: aaron@example.com salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test

activation_code: aaronscode # only if you’re activating new signups created_at: <%= 1.days.ago.to_s :db %> enabled: <%= true %>

disabled: id: 3 login: disabled email: disabled@example.com salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test #crypted_password: “ce2/iFrNtQ8=\n” # quentin, use only if you’re using 2-way encryption

created_at: <%= 5.days.ago.to_s :db %> activated_at: <%= 5.days.ago.to_s :db %> # only if you’re activating new signups enabled: <%= false %>