khiav223577 / deep_pluck

Allow you to pluck attributes from nested associations without loading a bunch of records.
MIT License
460 stars 14 forks source link

Rails 7.2 support: `undefined method "to_sym" for nil` #65

Closed yosiat closed 2 months ago

yosiat commented 2 months ago

Hi,

I am trying to upgrade my application to Rails 7.2, but when I use deep_pluck with association - I am getting the following error:

/Users/yosi/.rvm/gems/ruby-3.3.4/gems/activerecord-7.2.0/lib/active_record/reflection.rb:123:in `reflect_on_association': undefined method `to_sym' for nil (NoMethodError)

        normalized_reflections[association.to_sym]
                                          ^^^^^^^
        from /Users/yosi/.rvm/gems/ruby-3.3.4/gems/activerecord-7.2.0/lib/active_record/relation/delegation.rb:120:in `public_send'
        from /Users/yosi/.rvm/gems/ruby-3.3.4/gems/activerecord-7.2.0/lib/active_record/relation/delegation.rb:120:in `block in method_missing'
        from /Users/yosi/.rvm/gems/ruby-3.3.4/gems/activerecord-7.2.0/lib/active_record/relation.rb:1355:in `_scoping'
        from /Users/yosi/.rvm/gems/ruby-3.3.4/gems/activerecord-7.2.0/lib/active_record/relation.rb:541:in `scoping'
        from /Users/yosi/.rvm/gems/ruby-3.3.4/gems/activerecord-7.2.0/lib/active_record/relation/delegation.rb:120:in `method_missing'
        from /Users/yosi/.rvm/gems/ruby-3.3.4/gems/deep_pluck-1.3.1/lib/deep_pluck/model.rb:144:in `backtrace_possible_association'
        from /Users/yosi/.rvm/gems/ruby-3.3.4/gems/deep_pluck-1.3.1/lib/deep_pluck/model.rb:132:in `do_query'
        from /Users/yosi/.rvm/gems/ruby-3.3.4/gems/deep_pluck-1.3.1/lib/deep_pluck/model.rb:155:in `block in set_includes_data'
        from /Users/yosi/.rvm/gems/ruby-3.3.4/gems/deep_pluck-1.3.1/lib/deep_pluck/model.rb:198:in `load_data'
        from /Users/yosi/.rvm/gems/ruby-3.3.4/gems/deep_pluck-1.3.1/lib/deep_pluck/model.rb:155:in `set_includes_data'
        from /Users/yosi/.rvm/gems/ruby-3.3.4/gems/deep_pluck-1.3.1/lib/deep_pluck/model.rb:204:in `block in load_data'
        from /Users/yosi/.rvm/gems/ruby-3.3.4/gems/deep_pluck-1.3.1/lib/deep_pluck/model.rb:203:in `each'
        from /Users/yosi/.rvm/gems/ruby-3.3.4/gems/deep_pluck-1.3.1/lib/deep_pluck/model.rb:203:in `load_data'
        from /Users/yosi/.rvm/gems/ruby-3.3.4/gems/deep_pluck-1.3.1/lib/deep_pluck/model.rb:211:in `load_all'
        from /Users/yosi/.rvm/gems/ruby-3.3.4/gems/deep_pluck-1.3.1/lib/deep_pluck.rb:10:in `deep_pluck'
        from /Users/yosi/.rvm/gems/ruby-3.3.4/gems/deep_pluck-1.3.1/lib/deep_pluck.rb:16:in `deep_pluck'
        from bug.rb:52:in `<main>'

This ruby script reproduces the issue:

# frozen_string_literal: true

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  # Activate the gem you are reporting the issue against.
  gem "activerecord", "7.2"
  gem "pg"
  gem "deep_pluck"
end

require "active_record"
require "logger"
require "deep_pluck"

ActiveRecord::Base.establish_connection(
  adapter: "postgresql",
  database: "test",
  encoding: "unicode",
  host: "localhost",
  port: "5432",
  password: "12345",
  username: "test")

ActiveRecord::Schema.define do
  create_table :comments, force: true do |t|
    t.string :body
    t.integer :author_id
  end

  create_table :author, force: true do |t|
    t.string :name
  end
end

class Comment < ActiveRecord::Base
  belongs_to :author
end

class Author < ActiveRecord::Base
end

Comment.create! author: Author.create!(name: "Yosi"), body: "Hello"
Comment.create! author: Author.create!(name: "Yosi"), body: "World"

pp Comment.deep_pluck(:id, :body, author: :name)
khiav223577 commented 2 months ago

Thanks for your report!

It was caused by the behavior changes in Rails 7.2.0 See: https://github.com/rails/rails/pull/51726

In Rails 7.1.4 nil.to_s will become empty string. In Rails 7.2.0 nil.to_sym will raise error.

khiav223577 commented 2 months ago

Just released v1.3.2.

See the CHANGELOG for more details