ruby-on-railsactiverecordrails-activerecordruby-on-rails-4.2sti

Wrong class types used in sql when STI used with join query


I've recently upgraded from Rails 4.1.4 to 4.2.8 and I got very strange behavior. So we have table class Identity < ActiveRecord::Base and tables Doctor, Assistant, Substitute that are inherited from it.

So when I'm outside of any of those classes and do call (rails console, specs)

Doctor.all.to_sql => "SELECT \"identities\".* FROM \"identities\" WHERE \"identities\".\"type\" IN ('Doctor')" which is good.

When I do same call but from class method in Assistant model, it produces following Doctor.all.to_sql => "SELECT \"identities\".* FROM \"identities\" WHERE \"identities\".\"type\" IN ('Assistant', 'Substitute')" which is wrong.

While i'm inside class method in Assistant model and do

Doctor.unscoped.all => "SELECT \"identities\".* FROM \"identities\" WHERE \"identities\".\"type\" IN ('Doctor')" good query.

I've checked if we set default_scope somewhere but we don't. Also, it's quite tricky as when I do SomeTable.joins(:identities) I can't unscope identities which leads me to wrong query.

So, I'm not quite sure what's happened after upgrading, but I know it does works with old Rails 4.1.4.

Any hint would be helpful.

Regads


Solution

  • After upgrading to Rails 4.2.11, issue has gone. I will put some more effort to reproduce this behavior on older versions and to report to rails team if necessary.