has_and_belongs_to_many vs has_many through

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

    From http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many

    The simplest rule of thumb is that you should set up a has_many :through relationship if you need to work with the relationship model as an independent entity. If you don’t need to do anything with the relationship model, it may be simpler to set up a has_and_belongs_to_many relationship (though you’ll need to remember to create the joining table in the database). You should use has_many :through if you need validations, callbacks, or extra attributes on the join model.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 04:03

    As far as I can remember, has_and_belongs_to_many gives you a simple lookup table which references your two models.

    For example,

    Stories can belong to many categories. Categories can have many stories.

    Categories_Stories Table
    story_id | category_id
    

    has_many :through gives you a third model which can be used to store various other pieces of information which don't belong to either of the original models.

    For example

    Person can subscribe to many magazines. Magazines can have many subscribers.

    Thus, we can have a subscription model in the middle, which gives us a similar table to the earlier example, but with additional properties.

    Subscriptions Table
    person_id | magazine_id | subscription_type | subscription_length | subscription_date 
    

    And so on.

    0 讨论(0)
  • 2020-11-28 04:09

    Many of the answers clarify that you should use has_and_belongs_to_many vs. has_many through: if you will not need any extra data or validations on the join table.

    However, beware of taking this approach. In the early stages of application development, it is nearly impossible to know what extra features or validations you may need in the far future of your project's lifecycle. If you decided to use has_and_belongs_to_many, and want to add one simple datapoint or validation 2 years down the road, migrating this change will be extremely difficult and bug-prone. To be safe, default to has_many :through

    0 讨论(0)
  • 2020-11-28 04:12

    You should use has_many :through if you need validations, callbacks, or extra attributes on the join model.

    0 讨论(0)
  • 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.

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