Guest User

Untitled

a guest
Jun 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. class Model < ActiveRecord::Base
  2. has_many :model_options # a link table for many to many
  3. has_many :options,
  4. :through => :model_options,
  5. :dependent => :destroy,
  6. :foreign_key => 'model_id'
  7. end
  8.  
  9. model = Model.find(id)
  10. model.options.delete # also tried model.options.delete_all
  11.  
  12. model.options.each do |option|
  13. option.delete
  14. end
  15.  
  16. class Model < ActiveRecord::Base
  17. has_many :model_options # a link table for many to many
  18. has_many :options,
  19. :through => :model_options,
  20. :dependent => :destroy,
  21. :foreign_key => 'model_id'
  22.  
  23. # Clear options records before destroy
  24. before_destroy :clear_options
  25.  
  26. protected
  27. def clear_options
  28. options.clear
  29. end
  30. end
  31.  
  32. model.options.clear
Add Comment
Please, Sign In to add comment