ActiveRecord’s has_many and belongs_to methods both take a :foreign_key option. If I need to use it to handle a nonstandard FK column name, should I set it for the parent model (has_many), child model (belongs_to), or both, or does it matter?
ActiveRecord’s has_many and belongs_to methods both take a :foreign_key option. If I need to
Share
You should set the
:foreign_keyoption on both.Consider the following two models:
Declaring
has_manyin theArticleclass allows you to do:The
belongs_tomethod call in theCommentmodel allows for:As you can see, in both cases the foreign key is used. If omitted in either location, that particular association proxy will not work properly.