Hi all,
I think I might have found a bug in the 1.0.0 release. I've been
playing around with an app, updating it to rails 2.1, and that's meant
some gymnastics with the plugins I'm using. CPK is one of them.
Specifically, I'm getting a problem on line 60 of
association_preload.rb:
add_preloaded_records_to_collection(id_to_record_map[through_record[through_primary_key].to_s],
reflection.name, through_record.send(source))
This works great, unless the through_primary_key is itself a composite
key. When that happens, the code is trying to load
through_record[key_1,key_2], which returns nil, borking the whole
process.
I wish I could submit a patch to fix this, but I'm just not familiar
enough with either the AR internals, or the CPK patchwork overlay to
AR to really understand how any of this is working.
If it will help in constructing good tests, here's the model setup
I've got:
class Dorm < ActiveRecord::Base
has_many :rooms, :include => :room_attributes
end
class Room < ActiveRecord::Base
set_primary_keys :dorm_id, :room_number
belongs_to :dorm
has_many :room_attribute_assignments, :foreign_key =>
[:dorm_id, :room_number]
has_many :room_attributes, :through => :room_attribute_assignments
end
class RoomAttributeAssignment < ActiveRecord::Base
set_primary_keys :dorm_id, :room_number, :room_attribute_id
belongs_to :room, :foreign_key => [:dorm_id, :room_number]
belongs_to :room_attribute
end
class RoomAttribute < ActiveRecord::Base
has_many :room_atributes_assignments
has_many :rooms, :through
=> :room_attribute_assignments, :foreign_key =>
[:dorm_id, :room_number]
end
So if I do this:
d = Dorm.find(:first)
d.rooms
I get a "You have a nil object when you didn't expect it!" for the
reasons outlined above. Checking the server logs shows that all of
the SQL statements are being executed correctly, and are returning the
right things: the dorm loads, the rooms load, the
room_attribute_assignments load, and then the room_attributes load.
The SQL itself is fine: it's just the stitching together of the
different arrays that fails.
I'd appreciate any help anybody can offer in getting this issue
patched.
Thanks!