Rails app外键错误:管理用户不允许删除其他用户

Vir*_*ult 3 ruby sql ruby-on-rails heroku

当以管理用户身份登录时尝试删除用户时,我在heroku日志中收到以下错误:

2015-03-24T07:47:23.506661+00:00 app[web.1]: Started DELETE "/users/1" for 128.252.25.47 at 2015-03-24 07:47:23 +0000
2015-03-24T07:47:23.534256+00:00 app[web.1]:   SQL (4.4ms)  DELETE FROM "users" WHERE "users"."id" = $1  [["id", 1]]
2015-03-24T07:47:23.517508+00:00 app[web.1]:   User Load (1.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
2015-03-24T07:47:23.541747+00:00 app[web.1]: Completed 500 Internal Server Error in 31ms
2015-03-24T07:47:23.534529+00:00 app[web.1]: PG::ForeignKeyViolation: ERROR:  update or delete on table "users" violates foreign key constraint "fk_rails_274c57dd65" on table "cars"
2015-03-24T07:47:23.534534+00:00 app[web.1]: : DELETE FROM "users" WHERE "users"."id" = $1
2015-03-24T07:47:23.544385+00:00 app[web.1]: : DELETE FROM "users" WHERE "users"."id" = $1):
2015-03-24T07:47:23.544388+00:00 app[web.1]: 
2015-03-24T07:47:23.544384+00:00 app[web.1]: DETAIL:  Key (id)=(1) is still referenced from table "cars".
2015-03-24T07:47:23.544387+00:00 app[web.1]:   app/controllers/users_controller.rb:45:in `destroy'
2015-03-24T07:47:23.544390+00:00 app[web.1]: 
2015-03-24T07:47:23.536639+00:00 app[web.1]:    (1.7ms)  ROLLBACK
2015-03-24T07:47:23.544377+00:00 app[web.1]: 
2015-03-24T07:47:23.510694+00:00 app[web.1]: Processing by UsersController#destroy as HTML
2015-03-24T07:47:23.534532+00:00 app[web.1]: DETAIL:  Key (id)=(1) is still referenced from table "cars".
2015-03-24T07:47:23.510838+00:00 app[web.1]:   Parameters: {"authenticity_token"=>"iKb5lOUvx8qDoGHtVWNgYRL/dIk5zWFU5kiQpWtGOnabA+D7Yg1gj86NHwYKMmFeK5hh4F/3jndyKHHDHApVoQ==", "id"=>"1"}
2015-03-24T07:47:23.515104+00:00 app[web.1]:   User Load (2.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 2]]
2015-03-24T07:47:23.544382+00:00 app[web.1]: ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR:  update or delete on table "users" violates foreign key constraint "fk_rails_274c57dd65" on table "cars"
Run Code Online (Sandbox Code Playgroud)

我收到错误是因为用户has_many开车的桌子"汽车" ,所以我认为错误不是删除用户而是与用户关联的汽车.但我不明白如何修复我的数据库,以便删除用户.

这是我的模式:

schema.rb

create_table "cars", force: :cascade do |t|
    t.text     "year"
    t.text     "brand"
    t.text     "model"
    t.text     "vin"
    t.text     "mileage"
    t.integer  "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string   "picture"
  end

  add_index "cars", ["user_id"], name: "index_cars_on_user_id"

  create_table "users", force: :cascade do |t|
    t.string   "name"
    t.string   "email"
    t.datetime "created_at",                      null: false
    t.datetime "updated_at",                      null: false
    t.string   "password_digest"
    t.string   "phone"
    t.string   "remember_digest"
    t.boolean  "admin",           default: false
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["phone"], name: "index_users_on_phone", unique: true
Run Code Online (Sandbox Code Playgroud)

cod*_*ted 5

为那些不想删除相关记录的人添加替代方案(也许你想保留车记录供以后使用).

class User < ActiveRecord::Base
  ....
  has_many :cars, dependent: :nullify
end
Run Code Online (Sandbox Code Playgroud)

关联的记录foreign_key将设置为nil.

对我这样的人登陆你的问题非常有用!