在ActiveAdmin,當我要刪除我有以下的錯誤用戶:的ActiveRecord :: InvalidForeignKey在管理:: UsersController#破壞
ActiveRecord::InvalidForeignKey in Admin::UsersController#destroy
PG::ForeignKeyViolation: ERROR: update or delete on table "users" violates foreign key constraint "fk_rails_b080fb4855" on table "notifications" DETAIL: Key (id)=(15) is still referenced from table "notifications". : DELETE FROM "users" WHERE "users"."id" = $1
我已經有has_many :notifications, dependent: :destroy
在我的用戶模型,所以不瞭解錯誤。
通知模型:
class Notification < ActiveRecord::Base
before_validation :check_modeles
validates :message, presence: true
validates :modele, presence: true
validates :user_id, presence: true
validates :contact_id, presence: true
validates_associated :user
belongs_to :user
belongs_to :contact, :class_name => "User", :foreign_key => "contact_id"
用戶模式:
class User < ActiveRecord::Base
before_validation :check_genres
before_validation :set_image
before_validation :init_attrs
before_create :set_name
before_create :create_mangopay
after_create :set_centres_interets
after_create :set_preferences_musicales
after_create :check_on_create
after_update :check_on_update
before_update :create_mangopay_bank_account
before_destroy :remove_contact_notifications
acts_as_mappable :default_units => :kms,
:lat_column_name => :last_lat,
:lng_column_name => :last_lng
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
has_one :fil, dependent: :destroy
has_one :preferences_voyage, dependent: :destroy
has_one :verification, dependent: :destroy
has_many :badges, dependent: :destroy
has_many :favoris , dependent: :destroy
has_many :centres_interets, dependent: :destroy
has_many :preferences_musicales, dependent: :destroy
has_many :recommandations, dependent: :destroy
has_many :reputations, dependent: :destroy
has_many :reservations, dependent: :destroy
has_many :routes, dependent: :destroy
has_many :trajets_reguliers, dependent: :destroy
has_many :vehicules, dependent: :destroy
has_many :contact_notifications, foreign_key: 'contact_id', class_name: 'Notification', dependent: :destroy
has_many :notifications, dependent: :destroy
has_many :recherches, dependent: :destroy
has_many :blocages, dependent: :destroy
has_many :messageries, dependent: :destroy
has_many :messages, dependent: :destroy
validates :date_naissance, presence: true
validates :first_name, presence: true
validates :last_name, presence: true
也許has_many :notifications, dependent: :destroy
隻影響用戶,而不是接觸?如果是這樣,我該如何解決它?
我仍然有同樣的錯誤:/ –
我已經添加了另一點到我的答案,讓我知道是否有幫助! – SteveTurczyn
顯然,在ActiveAdmin中使用的方法是「刪除」,但我已經測試刪除用戶與依賴記錄,它刪除用戶與記錄,所以我不知道...在User模型中返回代碼返回同樣的錯誤,我很迷茫... –