Ruby on rails 使用Rails的敏捷Web开发:can';找不到id为1的产品
行项目模型的销毁单元测试失败,错误为“找不到id为1的产品”。Rails似乎无法销毁我的行_项,因为它在从数据库获取它时抛出异常。以下是我的行项目模型:Ruby on rails 使用Rails的敏捷Web开发:can';找不到id为1的产品,ruby-on-rails,ruby,testunit,Ruby On Rails,Ruby,Testunit,行项目模型的销毁单元测试失败,错误为“找不到id为1的产品”。Rails似乎无法销毁我的行_项,因为它在从数据库获取它时抛出异常。以下是我的行项目模型: class LineItem < ActiveRecord::Base belongs_to :product belongs_to :cart def total_price product.price * quantity end end 编辑:以下是产品型号: class Product < A
class LineItem < ActiveRecord::Base
belongs_to :product
belongs_to :cart
def total_price
product.price * quantity
end
end
编辑:以下是产品型号:
class Product < ActiveRecord::Base
has_many :line_items
before_destroy :ensure_not_referenced_by_any_line_item
# Validation
validates :title, :description, :image_url, presence: true
validates :title, length: {
minimum: 10,
message: "must be at least %{count} characters long"
}
validates :price, numericality: { greater_than_or_equal_to: 0.01 }
validates :title, uniqueness: true
validates :image_url, allow_blank: true, format: {
with: %r{\.(gif|jpg|png)$}i,
message: 'must be a URL for GIF, JPG or PNG image.'
}
private
def ensure_not_referenced_by_any_line_item
if line_items.empty?
return true
else
errors.add :base, 'Line items present'
return false
end
end
end
类产品
这是rails社区中工厂通常比固定装置更受欢迎的原因之一。固定装置不会自动加载其关联,因此容易损坏。您的夹具的产品id为1,但该产品不存在
我不确定如何解决您的特定问题,但我建议您:
product:tv
Breaking!
[1m[35m (0.1ms)[0m SELECT COUNT(*) FROM "line_items"
Processing by LineItemsController#destroy as HTML
Parameters: {"id"=>"980190962"}
[1m[36mLineItem Load (0.1ms)[0m [1mSELECT "line_items".* FROM "line_items" WHERE "line_items"."id" = ? LIMIT 1[0m [["id", "980190962"]]
[1m[35mProduct Load (0.1ms)[0m SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1 [["id", 1]]
Completed 500 Internal Server Error in 3ms
[1m[36m (0.1ms)[0m [1mrollback transaction[0m
[1m[35m (0.1ms)[0m begin transaction
[1m[36mLineItem Load (0.1ms)[0m [1mSELECT "line_items".* FROM "line_items" WHERE "line_items"."id" = ? LIMIT 1[0m [["id", 980190962]]
--- !ruby/object:LineItem
attributes:
id: 980190962
product_id: 1
cart_id: 1
created_at: 2012-05-25 20:37:17.000000000 Z
updated_at: 2012-05-25 20:37:17.000000000 Z
quantity: 1
product_price:
[1m[35mProduct Load (0.2ms)[0m SELECT "products".* FROM "products"
---
- !ruby/object:Product
attributes:
id: 207281424
title: Programming Ruby 1.9
description: Ruby is the fastest growing and most exciting dynamic language out
there. If you need to get working programs delivered fast, you should add Ruby
to your toolbox.
image_url: ruby.png
price: 49.5
created_at: 2012-05-25 20:37:17.000000000 Z
updated_at: 2012-05-25 20:37:17.000000000 Z
class Product < ActiveRecord::Base
has_many :line_items
before_destroy :ensure_not_referenced_by_any_line_item
# Validation
validates :title, :description, :image_url, presence: true
validates :title, length: {
minimum: 10,
message: "must be at least %{count} characters long"
}
validates :price, numericality: { greater_than_or_equal_to: 0.01 }
validates :title, uniqueness: true
validates :image_url, allow_blank: true, format: {
with: %r{\.(gif|jpg|png)$}i,
message: 'must be a URL for GIF, JPG or PNG image.'
}
private
def ensure_not_referenced_by_any_line_item
if line_items.empty?
return true
else
errors.add :base, 'Line items present'
return false
end
end
end