has_and_belongs_to_many vs has_many through

前端 未结 6 1435
没有蜡笔的小新
没有蜡笔的小新 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:00

    My rule of thumb is, can I get by with a list of checkboxes here? If so, then it's a habtm association. If I need the checkbox to capture more about the relationship than simply yes/no it belongs, then use has_many :through. HABTM is as simple as using the _ids method with a simple_form collection_check_boxes. Has_many :through often involves accepts_nested_attributes_for.

提交回复
热议问题