我有以下设置,模型客户端是模型联系人的子类。STI 表是联系人。
然后在测试代码中调用时出现错误
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 专家,但是如果客户表被寻址,则应该设置某种别名,或者应该通过其真实姓名联系人来寻址该表。
降序来自Client中的default_scope顺序
class Client < Contact
...
default_scope order: 'clients.name DESC'
end
这是在 rails 3.2.11 和 Postgres 9.1,pg gem 0.12.2 上。