sql - 在 Rails 中销毁用户会导致依赖模型出错
问题描述
我试图通过在用户控制器中调用@user.destroy 来删除我的rails 应用程序中的用户,但它向我显示了依赖模型的错误。
我正在使用设计,我不知道是否有另一种方法可以破坏那里的用户。我尝试更改为 delete_all 但没有工作然后在每个“has_many”行中删除用户模型上的“dependant::destroy”并在控制器中手动添加用于删除依赖于该用户的那些对象的代码但它显示我同样的错误。
用户.rb
class User < ApplicationRecord
extend Devise::Models
require 'uri'
has_one_attached :profile_pic
has_many :comments, dependent: :destroy
has_many :posts, dependent: :destroy
has_many :blockeds, class_name: 'block', foreign_key: 'blocked_id', dependent: :destroy
has_many :owner_blocks, class_name: 'block', foreign_key: 'owner_id', dependent: :destroy
has_many :followers, class_name: 'follow_user', foreign_key: 'followed_id', dependent: :destroy
has_many :following, class_name: 'follow_user', foreign_key: 'follower_id', dependent: :destroy
has_many :taggeds, class_name: 'tag', foreign_key: 'tagged_id', dependent: :destroy
has_many :taggers, class_name: 'tag', foreign_key: 'tagger_id', dependent: :destroy
has_many :achievements, through: :achievement_users, dependent: :destroy
has_many :downvote_comments, dependent: :destroy
has_many :upvote_comments, dependent: :destroy
has_many :upvote_posts, dependent: :destroy
has_many :downvote_posts, dependent: :destroy
has_many :follow_posts, dependent: :destroy
has_many :mark_innapropiateds, dependent: :destroy
评论.rb
class Comment < ApplicationRecord
belongs_to :user
belongs_to :post
users_controller.rb
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
迁移评论
class CreateComments < ActiveRecord::Migration[5.2]
def change
create_table :comments do |t|
t.references :user, foreign_key: true
t.references :post, foreign_key: true
t.string :content
t.timestamps
end
end
end
index.html.erb(我为用户调用destroy方法)
<% @users.each do |user| %>
<tr>
<td><%= user.nickname %></td>
<td><%= user.email %></td>
<td><%= user.role %></td>
<td><%= user.last_sign_in_at %></td>
<td><%= link_to 'Show', user, class: "btn btn-primary" %></td>
<td>
<% if user.role=='user' %>
<%= form_with(model: user, local: true, action: 'make_admin') do |form| %>
<%= form.hidden_field :role, value: 'admin' %>
<%= form.submit "Make Admin", class: 'btn btn-primary' %>
<% end %>
<% end %>
</td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-primary"%></td>
</tr>
<% end %>
我希望删除用户及其所有依赖对象(在这种情况下是他们的评论)。它只是向我显示评论错误,但我的猜测是它会向我显示其他模型的相同错误。这是错误:
SQLite3::ConstraintException: FOREIGN KEY constraint failed: DELETE FROM "comments" WHERE "comments"."id" = ?
删除 /users/1.json
def destroy
@user.destroy (<-- here is remarked)
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
参数:
{"_method"=>"delete", "authenticity_token"=>"tLUFwuWcM27OKSgrJLoxHTznr82DIFCPFF+InWYnNRpGRyqI5ZIrbUIIP0oaWCB8mVR5mIBI1SPS5E1cynOEmw==", "id"=>"1"}
解决方案
您的upvote_comments
和downvote_comments
仍然依赖于,comment
因此如果有任何引用评论,您将无法删除评论。
您需要添加dependent: :destroy
到您upvote_comments
的downvote_comments
评论模型中。
推荐阅读
- javascript - ?. 在打字稿中?
- javascript - Javascript 更新字段
- java - 在arraylist中存储同一父级的多个数据类型
- python - 端点 URL 无法访问 ConnectTimeoutError:端点 URL 上的连接超时:“https://glue.ap-south-1.amazonaws.com/”
- google-bigquery - 如何从 Bigquery 中的 Kmeans 聚类表中获取聚类数?
- html - Vue.js:在子组件的插槽中动态呈现 html
- buildpack - 是否可以自定义使用 Spring Native 生成的 docker 镜像(使用 buildpack)
- excel - 带有 BDH 功能自动化错误的 Bloomberg 文件
- azure - 在 Databricks 上使用 Pyspark 访问 Azure ADLS gen2
- c# - MongoDriver c# 按日期字段中的月份名称过滤