composite-primary-keys / composite_primary_keys

Composite Primary Keys support for Active Record
1.03k stars 350 forks source link

v8.1.6: bug in loading associations for a new record #478

Closed ainar-g closed 5 years ago

ainar-g commented 5 years ago
# CREATE TABLE as (id INT PRIMARY KEY, key TEXT);
# CREATE TABLE bs (id INT PRIMARY KEY, key TEXT);
class A < ActiveRecord::Base
  has_many :bs, foreign_key: :key, primary_key: :key
end
class B < ActiveRecord::Base
end

B.create!(id: 1, key: "foo")
a = A.new(key: "foo")
puts(a.bs.count)

Without the gem, it prints 1, as it should. With the gem, it's 0. The problem is an apparent misprint in lib/composite_primary_keys/associations/has_many_association.rb:

# CPK
# owner.attribute_present?(reflection.association_primary_key)
Array(reflection.klass.primary_key).all? {|key| owner.attribute_present?(key)}

It should be Array(reflection.association_primary_key).

cfis commented 5 years ago

Yup, sure looks like it. I don't maintain the 8.x branch anymore, but I'm can merge a pull request if you send one along.