在类 AI 中有两个作用域,s1 和 s2,它们都使用完全相同的连接列连接表 T:
named_scope :s1 :joins => "JOIN T on T.id = A.t_id", ...some conditions
named_scope :s2 :joins => "JOIN T on T.id = A.t_id", ...some other conditions
现在,这样做失败了:
A.s1.s2.all
错误:
ActiveRecord::StatementInvalid: Mysql::Error: Not unique table/alias: 'T'
我有点期望 Rails 对那些相同的连接很聪明,并且只需应用一次连接,但事实并非如此。我可以使用表别名,但是我仍然会毫无理由地拥有两个相同的连接。
我确定必须有一个适当的解决方案?