I have the following setup, model Client is subclass of model Contacts. The STI table is contacts.
Then in test code I get an error when calling
it "should have the right clients in the right order" do
@producer.clients.should == [a_client, b_client]
end
with the SQL statement incorrectly addressing the not existing clients table in the ORDER BY clause:
1) Producer clients associations should have the right clients in the right order
Failure/Error: @producer.clients.should == [a_client, b_client]
ActiveRecord::StatementInvalid:
PGError: ERROR: missing FROM-clause entry for table "clients"
LINE 1: ...lient') AND "contacts"."producer_id" = 6 ORDER BY clients.na...
^
: SELECT "contacts".* FROM "contacts" WHERE "contacts"."type" IN ('Client') AND "contacts"."producer_id" = 6 ORDER BY clients.name DESC
I am not enough of an SQL expert, but there should be some kind of alias set up if the clients table is addressed, or the table should be addressed by its real name contacts.
The descending order comes from the default_scope order in Client
class Client < Contact
...
default_scope order: 'clients.name DESC'
end
This is on rails 3.2.11 and Postgres 9.1, pg gem 0.12.2.