1

eコマースレール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 srmove 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