Ruby Tuesday
2011-May-08 21:33 UTC
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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Gregory Ma
2011-May-09 10:19 UTC
Re: How to get my cart destroy to run a line item destroy method
> event = @line_item.eventThis 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]) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.