STI与联接查询一起使用时,SQL中使用了错误的类类型

时间:2019-06-20 14:26:09

标签: ruby-on-rails activerecord rails-activerecord ruby-on-rails-4.2 sti

我最近从Rails 4.1.4升级到了4.2.8,我的行为很奇怪。 所以我们有桌子 class Identity < ActiveRecord::Base和从中继承的表Doctor, Assistant, Substitute

因此,当我不在这些课程中的任何一个并且要打电话(rails控制台,规格)

Doctor.all.to_sql => "SELECT \"identities\".* FROM \"identities\" WHERE \"identities\".\"type\" IN ('Doctor')"很好。

当我执行相同的调用但从Assistant模型中的类方法调用时,它会产生以下内容 Doctor.all.to_sql => "SELECT \"identities\".* FROM \"identities\" WHERE \"identities\".\"type\" IN ('Assistant', 'Substitute')",这是错误的。

虽然我处于Assistant模型的类方法之内,但可以做

Doctor.unscoped.all => "SELECT \"identities\".* FROM \"identities\" WHERE \"identities\".\"type\" IN ('Doctor')"很好的查询。

我已经检查了是否将default_scope设置在某个地方,但没有设置。而且,当我这样做时,这非常棘手 SomeTable.joins(:identities)我无法取消范围identities的范围,这导致我输入错误的查询。

因此,我不确定升级后会发生什么,但是我知道它确实适用于旧的Rails 4.1.4。

任何提示都会有所帮助。

Regads

1 个答案:

答案 0 :(得分:0)

升级到Rails 4.2.11后,问题不再存在。 我将花更多的精力在旧版本上重现此行为,并在必要时向Rails团队报告。