Ask A Question

Notifications

You’re not receiving notifications from this thread.

the deleting user is blocked if they have subscribed to forum

Vedant Jain asked in Rails

We have used the simple_discussion gem in our rails application but the error is caused when the user is destroyed.

PG::ForeignKeyViolation: ERROR: update or delete on table "users" violates foreign key constraint "fk_rails_bc51c711fd" on table "forum_threads"
DETAIL: Key (id)=(1) is still referenced from table "forum_threads".
Reply

I don't know how simple_discussion works, but the error by itself tell us what is going on.

Probably, you have a "belongs_to :user", so you cant delete a user because this relation.

I can see two way to handle it:

First -> when deleting a user, delete all forum_threads related to this user. At Users model, add "has_many :forum_threads, dependent: :destroy".

Second -> when deleting a user, nullify user_id at forum_threads table. At Users model, add "has_many :forum_threads, dependent: :nullify"

https://guides.rubyonrails.org/association_basics.html#options-for-has-many-class-name

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 81,842+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.