I believe the problem is that the id isn't specified in the YAML fixture; it was assumed that the id would be in the order that the records appeared in the YAML file. However, that isn't always the case. This has been commented on a number of times in this forum.
You have three options I think:
- Add ids to the YAML files
- Test on created_at
- Test for presence of
Add ids to the YAML files
Probably the simplest option, and personally the one I'd do. Just add a line to the fixture for each record. For example, for the first record:
Obviously you increment the digit for each record (so the next item has id: 2 and so on).
That way you know what is going into the database, which makes it easier to track errors (for example when tracing problems with foreign keys). Personally, I always specify ids in my fixtures.
Test on created_at
Update the stories has_many relationship in the User model so that it returns storied in the order they were created:
has_many :stories, :order => 'created_at'
Then user.stories.first will return the stories with the earliest create_at first
[*]Test for presence of
If you aren't really bothered about the order and just want to make sure that the stories contain a particular story, you can change the second line to: