1

我的问题是,在我的电子商务 rails 3.0.x 应用程序中,我有产品、line_items 和购物车。

line_item 属于购物车和产品。一个产品可以有很多 line_items。如果我删除一个产品,line_items 也应该被删除 (:dependent => :destroy)

现在,奇怪的是,当我从购物车中删除一个 line_item 时,这也会删除我的完整产品记录!

这是必要的代码:

#cart_view.erb
<% for line_item in cart.line_items %>

<!-- HTML omitted -->

<!-- This link actually removes not only the line_item from cart, but also deletes the product...? . -->
<%= link_to "remove", line_item, :confirm => 'Remove from cart?', :method => :delete %>

<% end %>

#product.rb
class Product < ActiveRecord::Base
   # for cart
   has_many :line_items, :dependent => :destroy
   has_many :stock_items, :dependent => :destroy
end

#line_item.rb
class LineItem < ActiveRecord::Base

  belongs_to :product

end

# stock_item.rb
class StockItem < ActiveRecord::Base

  belongs_to :product, :dependent => :destroy

  has_many :line_items

end


#line_items_controller.rb

class LineItemsController < ApplicationController


   def destroy
     @line_item = LineItem.find(params[:id])
     @cart = @line_item.cart
     @line_item.destroy
     flash[:notice] = "Removed from cart."
     redirect_to @cart
   end
end

这种行为并不总是这样,至少我不记得了。乍一看,我在这里没有任何问题,因此任何想法都受到高度赞赏。

编辑

db 表定义(我使用 mysql2 db 适配器):

| line_items | CREATE TABLE `line_items` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `unit_price` int(11) NOT NULL DEFAULT '0',
  `product_id` int(11) DEFAULT NULL,
  `cart_id` int(11) DEFAULT NULL,
  `quantity` int(11) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `stock_item_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `line_items_cart_id_fk` (`cart_id`),
  CONSTRAINT `line_items_cart_id_fk` FOREIGN KEY (`cart_id`) REFERENCES `carts` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 |

| products | CREATE TABLE `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `description` text,
  `price` int(11) NOT NULL DEFAULT '0',
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `title` varchar(255) DEFAULT NULL,
  `subtitle` varchar(255) DEFAULT NULL,
  `currency` varchar(255) DEFAULT 'GBP',
  `legacy` tinyint(1) DEFAULT '0',
  `product_code` varchar(255) DEFAULT NULL,
  `category_id` int(11) DEFAULT NULL,
  `deleted_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 |

** 编辑 2 **

rails s当我点击 rmove line_item 链接时的日志:

Started POST "/line_items/16?locale=en" for 127.0.0.1 at 2012-11-27 16:51:06 +0100
  SQL (1.6ms)  describe `roles_users`
  Processing by LineItemsController#destroy as HTML
  Parameters: {"authenticity_token"=>"sN78OIdiVW5WgSoLA3JR7RFOWIy5B+j1XLqe47vZf3I=", "locale"=>"en", "id"=>"16"}
  LineItem Load (0.4ms)  SELECT `line_items`.* FROM `line_items` WHERE `line_items`.`id` = 16 LIMIT 1
  Cart Load (0.3ms)  SELECT `carts`.* FROM `carts` WHERE `carts`.`id` = 16 LIMIT 1
  SQL (0.1ms)  BEGIN
  AREL (0.2ms)  DELETE FROM `line_items` WHERE `line_items`.`id` = 16
  StockItem Load (0.5ms)  SELECT `stock_items`.* FROM `stock_items` WHERE `stock_items`.`id` = 7 LIMIT 1
  AREL (0.3ms)  DELETE FROM `stock_items` WHERE `stock_items`.`id` = 7
  Product Load (0.7ms)  SELECT `products`.* FROM `products` WHERE `products`.`id` = 7 LIMIT 1
  Product::Translation Load (0.7ms)  SELECT `product_translations`.* FROM `product_translations` WHERE (`product_translations`.product_id = 7)
  AREL (0.3ms)  DELETE FROM `product_translations` WHERE `product_translations`.`id` = 11
  AREL (0.2ms)  DELETE FROM `product_translations` WHERE `product_translations`.`id` = 12
  ProductLike Load (0.4ms)  SELECT `product_likes`.* FROM `product_likes` WHERE (`product_likes`.product_id = 7)
  Asset Load (0.4ms)  SELECT `assets`.* FROM `assets` WHERE (`assets`.product_id = 7)
  AREL (0.3ms)  DELETE FROM `assets` WHERE `assets`.`id` = 6
  SetItem Load (0.4ms)  SELECT `set_items`.* FROM `set_items` WHERE (`set_items`.product_id = 7)
  StockItem Load (0.3ms)  SELECT `stock_items`.* FROM `stock_items` WHERE (`stock_items`.product_id = 7)
  AREL (0.2ms)  DELETE FROM `products` WHERE `products`.`id` = 7
  SQL (0.6ms)  COMMIT
Redirected to http://localhost:3000/carts/16?locale=en
Completed 302 Found in 3093ms
/Users/admin/.rvm/gems/ruby-1.9.2-p136@rails3/gems/activesupport-3.0.17/lib/active_support/core_ext/string/output_safety.rb:23: warning: regexp match /.../n against to UTF-8 string
4

0 回答 0