views:

1239

answers:

3

I'm using an STI model with a single "Accounts" table to hold information for Users and Technicians (i.e. User < Account, Technician < Account). Everything works from a functional perspective, but things explode when running unit tests:

... 8) Error: test_the_truth(UserTest): ActiveRecord::StatementInvalid: PGError: ERROR: relation "technicians" does not exist : DELETE FROM "technicians" ...

Essentially, the standard framework doesn't recognize that the Technicians and Users tables (or "relations" as PostgreSQL calls them) don't exist and, in fact, should be aliased to Accounts.

Any ideas? I'm relatively new to RoR and I'm at a loss as how to fix this without ripping out STI all together.

A: 
  • Did you make sure your accounts table includes a 'type' column? You need one in order to make STI work.
  • I've actually had some database compatibility trouble with a column called 'type' and have occasionally switched to 'kind' to alleviate that. That might be the problem; try setting self.inheritance_column = "kind" in the base class (Account) and see if that helps.
Brian Guthrie
Accounts did include a "type" column and, unfortunately, your suggestion to change its name to "kind" didn't help; the problems persist.
D Carney
+5  A: 

Turns out that the problem was due to the presence of:

./test/fixtures/technicians.yml ./test/fixtures/users.yml

This makes sense as the framework expected to be able to insert data into similarly named tables.

D Carney
<3 stack overflow. Wish I could mod you up more!
nfm
A: 

I have had a similar issue that was resolved by removing the YAML file for the child model. Essentially rails is looking at the fixtures created in /test/fixtures/ and trying to empty the tables for each so that it can reload them for you.

In my case I had run the script/generate model command which automatically creates a new fixture. Then I changed the model to inherit from the proper parent class. Well, since the fixture still existed, rails was trying to DELETE FROM child before loading the fixtures.

If you really do need to preload data you should use the parent models fixture and set the type field to the proper model name.

Robert Jackson