4

I feel like I'm missing something very obvious, but what I'm doing is testing the destroy route on my User model.

When I try and @user.destroy, I get the following exception:

*** ActiveRecord::InvalidForeignKey Exception: SQLite3::ConstraintException: FOREIGN KEY constraint failed: DELETE FROM "users" WHERE "users"."id" = ?

The only problem is, I already have dependent: :destroy in place for the has_many association, and I have also tried :delete_all to no avail. This is the only association I have to this model right now.

Any ideas? I'm sure its something simple

Tyler
  • 19,113
  • 19
  • 94
  • 151
  • Please read [this accepted answer](https://stackoverflow.com/questions/1896777/cascade-delete-in-ruby-activerecord-models). I propose that your setup has a problem such that the `dependent` option is not working. – Tim Biegeleisen Dec 30 '17 at 05:44
  • Does not seem to be the case, I do not have a `through` dependency – Tyler Dec 30 '17 at 13:01
  • Do you have another object created in your test that references the user? I just ran into a similar issue when I didn't have my relationships ordered properly. – Caleb Huitt - cjhuitt Feb 25 '18 at 19:05

1 Answers1

1

Check your schema to see if you have other relations in your model, if you have more relations use the dependent: :destroy, also showing the schema to see the relations would help to see if other models have a relation with your model.

Pedro
  • 1,352
  • 2
  • 10
  • 22