How to get my cart destroy to run a line item destroy method

Hello,

Complete novice here. I have a line_item that once created or updated
calculates the capacity left and displays it in the events view. The
Add to Cart button triggers the create line_item method. I’ve added
further capacity calculations to the line_item destroy method. My
problem is that the line_item destroy method is not called when the cart
is destroyed.

class Cart < ActiveRecord::Base

has_many :line_items, :dependent => :destroy

The line items are destroyed but the functionality in the line_item
destroy method does not run.

Line Item Controller ----

def destroy
@line_item = LineItem.find(params[:id])
event = @line_item.event
new_quantity = params[:line_item_quantity].to_i
change_in_capacity = @line_item.quantity + new_quantity

respond_to do |format|

  @line_item.destroy
  event.capacity = change_in_capacity
  event.save

So I tried to add a call to it in the Cart Controller as follows ----

def destroy
@cart = Cart.find(params[:id])
@line_item = LineItem.find(params[:cart_id])
@cart.destroy
@line_item.destroy
session[:cart_id] = nil

but I can’t seem to find the line item this way

“Couldn’t find LineItem without an ID”

Any ideas? Thanks in advance from a complete beginner.

event = @line_item.event

This can’t work because the event is associated to a line_item. You have
to clone the event if you want to use it after deleting the line_item.
event = @line_item.event.clone

@line_item = LineItem.find(params[:cart_id])

This should be LineItem.find_by_cart_id(params[:cart_id])