[英]Rails polymorphic association error on user destroy
I'm trying to link notifications to users like so. 我正在尝试将通知链接到这样的用户。
belongs_to :notified_by, class_name: 'User'
My user has_many notifications. 我的用户has_many个通知。
has_many :notifications, dependent: :delete_all
The problem is, when I try to destroy a user, I get an error when it goes to delete all the notifications. 问题是,当我尝试销毁用户时,删除所有通知时会收到错误消息。
ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column notifications.user_id does not exist)
LINE 1: DELETE FROM "notifications" WHERE "notifications"."user_id" ...
There is no user_id in the Notifications table, just a notified_by_id. 通知表中没有user_id,只有notify_by_id。 I have tried using that in place of notified_by, but that didn't work either. 我尝试用它来代替notify_by,但是那也不起作用。
我必须在用户模型中设置外键,以将notify_by_id识别为要查找的列。
has_many :notifications, foreign_key: 'notified_by_id', dependent: :delete_all
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.