Posts

Showing posts from May, 2016

Delete Vs. Destroy In Rails

I’ve been writing this web app that has the following models: Order, Recipient, Message. An order has many recipients and a recipient has many messages. The recipients are also dependent upon the order and the messages are dependent upon the recipient. In Ruby code this looks like: Order.rb class Order < ActiveRecord::Base has_many :recipients, :dependent => :destroy has_many :messages, :through => :recipients end Recipient.rb class Recipient < ActiveRecord::Base belongs_to :order has_many :messages, :dependent => :destroy end The idea here is that when I delete an order, I also delete any associated recipients and any associated messages. My controller looked like this: def delete Order.delete(params[:id]) end There are a couple things wrong with this. The first and most important thing is that when I delete a row in the order table, it leaves orphaned rows in the order and messages table. I want to delete these rows as well