首页 > 解决方案 > 当 STI 与连接查询一起使用时,sql 中使用了错误的类类型

问题描述

我最近从 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')"这很好。

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

当我在助手模型中的类方法中并做

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

我检查了我们是否在某处设置了 default_scope,但我们没有。此外,这非常棘手,因为当我这样做时, SomeTable.joins(:identities)我无法取消范围identities,这会导致我查询错误。

所以,我不太确定升级后发生了什么,但我知道它确实适用于旧的 Rails 4.1.4。

任何提示都会有所帮助。

Regads

标签: ruby-on-railsactiverecordrails-activerecordruby-on-rails-4.2sti

解决方案


升级到 Rails 4.2.11 后,问题消失了。我将付出更多努力在旧版本上重现这种行为,并在必要时向 Rails 团队报告。


推荐阅读