1

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.

  • Did you ever find a solution to this? I think I have the same problem with Rails not understanding when one of the 'M' tables in M-1-M is an inherited table. http://stackoverflow.com/questions/39524488/rails-5-object-relation-impedance-and-how-to-structure-multiple-inherited-clas – rmcsharry Sep 24 '16 at 07:46

1 Answers1

1

Just found it myself:

The default scope should refer to contacts.name, as that seems to be part of a SQL statement that is forwarded as is to the database.