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

Does not handle polymorphic associations correctly #13

Closed Bogadon closed 7 years ago

Bogadon commented 7 years ago

Great gem, thank you for sharing! Very convenient when you need to build classes composed of data from a whole bunch of tables, and using models is too slow.

It does not handle polymorphic associations correctly; the '*_type' column is ignored, so you may get the wrong data in situations where two different parent models share the same id.

For example:

class Child < ApplicationRecord
  belongs_to :parent, polymorphic: true
end

class One < ApplicationRecord
  has_one :child, as: :parent
end

class Two < ApplicationRecord
  has_one :child, as: :parent
end

One.create(id: 1, child: Child.new)
Two.create(id: 1, child: Child.new)

Two.all.deep_pluck(child: [:id]) == One.all.deep_pluck(child: [:id]) # Should not be true!

I will try and figure out a PR.

khiav223577 commented 7 years ago

Just release a new version 1.0.3 and this bug should be fixed. Thanks for your PR 😄