has_and_belongs_to_many vs has_many through

前端 未结 6 1433
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 03:47

Please explain the difference between has_and_belongs_to_many and has_many through relationship. When and where to use which one?

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 04:18

    From my experience it's always better to use has_many: through because you can add timestamps to the table. Many times while debugging some ActiveRecord objects that are connected through HABTM, I was missing created_at, updated_at timestamps to get the clue what actually happened. So keep in mind that it can help you to debug, investigate issues with data relations in context of time, because without it your are "blind" when relations were created or updated.

提交回复
热议问题