I have two models: store and category with a joining table called categories.stores.
How could I delete all relationship data for a store object in the joining table?
Could I use something like one of these:
store.categories.destroy or category.stores.destroy
Note: both models are has_and_belongs_to_many (and thus have no identifier for each association record — only store_id and category_id)
In a
has_and_belongs_to_manyassociation you can either usedelete_allordestroy_all.In a
has_manyassociation you should consider usingdelete_allbecause it deletes records following the:dependentstrategy (it nullifys foreign keys by default) instead ofdestroy_allwhich destroys associated records.More details at http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Delete+or+destroy%3F