Search code examples
ruby-on-rails-2fixtures

How does a Rails fixture find its model class?


I've created a model that changes its table name, and now the fixture doesn't seem to be able to find it. Is there something I need to do to tell the fixture which model to use?

I'm using Rails 2.0.5.

The symptom is that when I run rake db:fixtures:load, I get an error that the id field cannot be null.

It's weird, because I have another model that is changing its table name, and it works fine.

I have two tables: customer and long_prefix_orders. They both have required id fields.

I have two models that look something like this:

# app/models/customer.rb
class Customer < ActiveRecord::Base
  set_table_name :customer
end

# app/models/order.rb
class Order < ActiveRecord::Base
  set_table_name :long_prefix_orders
end

I have two fixtures that look something like this:

# test/fixtures/customer.yml
one:
  name: Bob

# test/fixtures/long_prefix_orders.yml
one:
  name: Party Supplies
  customer_id: one

The customer fixture works, and the order fixture doesn't. Is changing the table name from plural to singular a small enough change that the fixture can find it, or is there some other configuration that I've forgotten about?


Solution

  • I think the problem here was that I was using customer_id instead of customer when I referred to the parent record. Use the name of the relationship in fixtures, not the name of the database field.