I have opened a PR which adds tests for this type of problem (selecting the primary key of a self-referencing association, causing the wrong side of the association being selected). Christian Beikov is reworking the table-reference resolution code, hopefully his changes will improve the current logic and also fix some bugs including this one. |