Wow I\'ve been struggling with this for whole day, following the \"official\" ruby on rails guides document, and just discovered that I might have been misguided by the document
That's right, the guide document is incorrect at the time of this writing.
The belongs_to
doesn't need the :foreign_key
option because AR will infer manager_id
from the name of the association ("manager"). As documented, AR would raise an error when, given an Employee @dwight
one attempts to @dwight.subordinates
, because AR would use employee_id
in the WHERE condition of the SELECT statement.
According to the AR documentation passing the :foreign_key
option to has_many
results in declaring the FK that will be used when generating the query for @dwight.subordinates
.