I have a table for notifications, which stores the ID from several Models, so a user gets a notification upon actions related to those (someone posted a comment on your article). The notifications do not "belong to" anything but a user it's going to, though, it's just referencing the IDs of things. So now whenever I try to delete an article, for example, I get an error:
ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: update or delete on table "comments" violates foreign key constraint "fk_rails_9268535f02" on table "notifications"
How can I set it up to delete any referenced notifications also?
My db schema for notifications:
create_table "notifications", force: :cascade do |t|
t.integer "recipient_id"
t.integer "notified_by_id"
t.integer "glip_id"
t.integer "article_id"
t.integer "group_id"
t.integer "post_id"
t.integer "comment_id"
t.integer "notation_id"
t.integer "response_id"
t.integer "remark_id"
t.integer "conversation_id"
t.integer "message_id"
t.boolean "read", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "notification_type"
t.index ["article_id"], name: "index_notifications_on_article_id"
t.index ["comment_id"], name: "index_notifications_on_comment_id"
t.index ["conversation_id"], name: "index_notifications_on_conversation_id"
t.index ["glip_id"], name: "index_notifications_on_glip_id"
t.index ["group_id"], name: "index_notifications_on_group_id"
t.index ["message_id"], name: "index_notifications_on_message_id"
t.index ["notation_id"], name: "index_notifications_on_notation_id"
t.index ["notified_by_id"], name: "index_notifications_on_notified_by_id"
t.index ["post_id"], name: "index_notifications_on_post_id"
t.index ["recipient_id"], name: "index_notifications_on_recipient_id"
t.index ["remark_id"], name: "index_notifications_on_remark_id"
t.index ["response_id"], name: "index_notifications_on_response_id"
end
And my notifications Model:
class Notification < ApplicationRecord
enum notification_type: { comment: 0, notation: 1, message: 2, feature: 3, marked_helpful: 4,
participation: 5, response: 6, remark: 7, follow: 8, unfollow: 9 }
enum read: { read: true, unread: false }
belongs_to :notified_by, class_name: 'User'
end