2013-03-12 81 views
1

我有以下设置,模型客户端是模型联系人的子类。 STI表是联系人。rails,SQL,单表继承,表寻址为“子类”而不是类

然后在测试代码中调用

it "should have the right clients in the right order" do 
    @producer.clients.should == [a_client, b_client] 
end 

用SQL语句错误地解决不存在客户表中的ORDER BY子句时,我得到一个错误:

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 

我不够的一个SQL专家,但如果客户端表被寻址,则应该设置某种别名,或者该表应该由其真实姓名联系人来解决。

从高到低的顺序来自default_scope为了在客户端

这是在轨道上3.2.11和Postgres 9.1,PG宝石0.12.2。

+0

你有没有找到这个解决方案?我想我和Rails有同样的问题,当M-1-M中的一个'M'表是一个继承表时,我不知道这个问题。 http://stackoverflow.com/questions/39524488/rails-5-object-relation-impedance-and-how-to-structure-multiple-inherited-clas – rmcsharry 2016-09-24 07:46:04

回答

1

只是觉得自己:

默认的范围应参照contacts.name,因为这似乎是被转发,是对数据库的SQL语句的一部分。