Reputation: 15986
I have a User
model and a UsageRecord
model that has a foreign key of a user_id
. My YAML fixtures are like this:
test/fixtures/users.yml
user_one:
email: [email protected]
test/fixtures/usage_records.yml
one:
user_id: user_one
If I run rake test
a test fails because the user_id
on a UsageRecord
is not valid. If I run RAILS_ENV=test rails c
and examine the database, sure enough the user_id
of the only UsageRecord
has an ID of 0 (which doesn't match the user ID in the Users
table).
What am I doing wrong here? I want it to have the user ID of 293267324 which matches the generated User
model.
UPDATE I found the problem. Instead of specifying user_id
, I had to use the association name instead. Switching the UsageRecord
YAML to user: one
works.
Upvotes: 1
Views: 544
Reputation: 15986
The trick is to use the association name not the column name that will end up having the value. For instance, instead of setting user_id
for the UsageRecord
, set the user
. This will perform the linking properly.
Upvotes: 1