views:

229

answers:

2

I'm having problems with my fixtures in Rails.

I have two models, a message and a user. The user model is generated from restful authentication. A message has a sender and a recipient, which are both users.

When I don't use the fixtures everything works fine. But when I try to use them in fixtures both are nil.

This is the messages fixtures file:

first_message_for_quentin:
    title: First message
    body: This is your first message.
    recipient: quentin
    sender: aaron

quentin and aaron are the user fixtures which were generated by restful authentication.

Is there a way I can use the generated ids there or do I have to do it another way?

+3  A: 

The ActiveRecord Fixtures creates the ID column for each row by calling Fixtures.identify on the label you gave the row in the yaml.

So you should be able to do something like the following:

first_message_for_quentin:
    title: First message
    body: This is your first message.
    recipient_id: <%= Fixtures.identify(:quentin) %>
    sender_id: <%= Fixtures.identify(:aaron) %>

You might want to look at some of the docs for fixture label interpolation.

csexton
Will try this later. Thanks for the docs.
Thijs Wouters
This didn't work. I dug somewhat deeper and found the real problem.
Thijs Wouters
A: 

The problem was in the fixture file which was created by restful authentication.

quentin:
    id: 1
    ...

aaron:
    id: 2
    ...

I removed these ids and everything is working fine.

Thijs Wouters
Ahh, that would do it.
csexton