Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails Single Table Inheritance (STI) and unit test problem (with PostgreSQL)

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.

like image 443
David Carney Avatar asked Mar 14 '09 18:03

David Carney


2 Answers

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.

like image 54
David Carney Avatar answered Nov 12 '22 07:11

David Carney


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.

like image 45
Robert Jackson Avatar answered Nov 12 '22 06:11

Robert Jackson