deep_pluck icon indicating copy to clipboard operation
deep_pluck copied to clipboard

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

Open yosiat opened this issue 6 months ago • 0 comments

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)

yosiat avatar Aug 19 '24 10:08 yosiat