Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 3.2.22.2 - Ruby 2.3 - Hash now responds to `to_proc` and `interpolate` blows up #25010

Closed
tlrdstd opened this issue May 13, 2016 · 4 comments

Comments

Projects
None yet
8 participants
@tlrdstd
Copy link

commented May 13, 2016

Background

As of Ruby 2.3, Hash responds to to_proc This is a change from Ruby 2.2.

In ActiveRecord 3.2.22.2, the interpolate method (duplicated across ActiveRecord::Associations::JoinDependency::JoinAssociation, ActiveRecord::Associations::Association, and ActiveRecord::Associations::Preloader::Association) checks its input to see if it responds to to_proc.

I've seen a few bug reports on other projects that seem to be caused by this issue, but Casecommons/pg_search/issues/297 is the clearest.

Steps to reproduce

class Document < ActiveRecord::Base
  has_many :images, as: :parent
end

Calling Document.joins(:images).count triggers the error.

Expected behavior

In Ruby 2.2, if passed a Hash, this check returned false, and all was well - the query returns a count.

Actual behavior

In Ruby 2.3, if passed a Hash, this check returns true, and interpolate returns nil. This nil eventually gets passed to Arel.sql() and you get a no implicit conversion of nil into String error.

System configuration

Rails version: 3.2.22.2

Ruby version: 2.3.0

Temporary workaround

I've undefined to_proc on Hash, as I'm quite certain I have no code that relies on that:

class Hash
  undef_method :to_proc if self.method_defined?(:to_proc)
end

@tlrdstd tlrdstd changed the title Rails 3.2.22.2 - ActiveRecord::Associations::JoinDependency::JoinAssociation - Bug on Ruby 2.3 Rails 3.2.22.2 - Ruby 2.3 - Hash now responds to `to_proc` and `interpolate` blows up May 13, 2016

@arthurnn arthurnn self-assigned this May 13, 2016

@arthurnn

This comment has been minimized.

Copy link
Member

commented May 13, 2016

@tlrdstd thanks for letting us know.
We dont support rails 3.2 anymore. But i've been responsible for patching security level concerns there.
This is not a security issue, so we probably wont release a new version for it, however if you wanna work in a PR to fix it, i would be glad to merge it to 3-2-stable branch.

Thanks

@jhanggi

This comment has been minimized.

Copy link

commented May 13, 2016

I was running into a similar issue when testing against Ruby 2.3/Rails 3.2.22.2.

I have an association that could be soft-deleted, but when upgrading to 2.3, the conditions were no longer being respected and deleted items were being returned.

Essentially:

class Product < ActiveRecord::Base
  has_many   :accessories, conditions: { deleted_at: nil }, dependent: :destroy
end

Thanks for digging in and finding out why. Your temporary workaround solves the problem for me as well.

@tlrdstd

This comment has been minimized.

Copy link
Author

commented May 16, 2016

I've started working on a patch for this. Ran out of time this weekend, but at a quick glance, it does appear that several of the sqlite3 tests for ActiveRecord pass under Ruby 2.2 but fail under Ruby 2.3, which gives me hope for uncovering all the places affected by Hash.to_proc. I'll keep working on it this week.

tlrdstd added a commit to tlrdstd/rails that referenced this issue May 16, 2016

huerlisi added a commit to huerlisi/bookyt that referenced this issue Sep 7, 2016

chore(ruby): downgrade ruby version to 2.2.5
We're still using Rails 3.2 which does break on Ruby 2.3 on same cases.

This patch downgrades the ruby version to 2.2.5 in `.ruby-version`.

See: rails/rails#25010
@nodanaonlyzuul

This comment has been minimized.

Copy link

commented Oct 28, 2018

I just want to say thanks to @tlrdstd.
This ticket (and your PR!) just saved me a night of head scratching.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.