我正在做一个Ruby on Rails课程,我正在试图删除一个项目组合项目,但它给了我一个错误。这是错误。
ActiveRecord::InvalidForeignKey in PortfoliosController#destroy
PG::ForeignKeyViolation: ERROR: update or delete on table "portfolios" violates foreign key constraint "fk_rails_cc5ab4a1c3" on table "technologies" DETAIL: Key (id)=(12) is still referenced from table "technologies". : DELETE FROM "portfolios" WHERE "portfolios"."id" = $1
Extracted source (around line #52):
50
51 # Destroy/delete the record
52 @portfolio_item.destroy
53
54 # Redirect
55 respond_to do |format|`
Rails.root: /home/ubuntu/workspace/DevcampPortfolio/ClonedRepo/Devcamp-Portfolio
Application Trace | Framework Trace | Full Trace
app/controllers/portfolios_controller.rb:52:in 'destroy'
Request
Parameters:
{"_method"=>"delete", "authenticity_token"=>"3fGuZqFrTblpS7aQGpfszjN24lSxu6SAr5/JRlLc0RDupDZ7BJKJr6kPnEYHa0/BR7rmTPnN+/i5Ptjb3wCLtw==", "id"=>"12"}
Response
Headers:
None
the code for the place I am in the course
如何更改以修复它?
答案 0 :(得分:1)
您需要将依赖::: destroy添加到投资组合模型的has_many关系中。
has_many:technologies,dependent :: destroy 这将使你在销毁投资组合时摧毁所属的技术。投资组合。
如果您希望保留所需的技术来更改它们之间的关系,或者允许它们成为无父对象并将before_destroy操作挂钩到投资组合模型。