Ruby on Rails guides document on Activerecord association incorrect?

后端 未结 1 520
终归单人心
终归单人心 2021-02-07 06:15

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

相关标签:
1条回答
  • 2021-02-07 06:53

    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.

    0 讨论(0)
提交回复
热议问题