0

I'm new to Ruby on Rails and I'm trying to build a simple web application.

  • I have an 'article' model and a 'comment' model
  • The 'article' can have many 'comments'. I deployed my web on fly.io. When I create an article and add comment to that article, I can not delete the article and the error said that 'violate foreign key constraints'. So I thought adding "dependent: :destroy" would solve the problem but it's not, still the same error. Did I miss something? Do I need to run any migration ? Below is part of my code.

    class Article < ApplicationRecord

    has_many :comments, dependent: :destroy
    
end
   class Comment < ApplicationRecord
  belongs_to :article
end

UPDATED: Below is my schema file

ActiveRecord::Schema[7.0].define(version: 2023_06_22_082322) do
  create_table "articles", force: :cascade do |t|
    t.string "title"
    t.text "body"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "comments", force: :cascade do |t|
    t.string "commenter"
    t.text "body"
    t.integer "article_id", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["article_id"], name: "index_comments_on_article_id"
  end

  add_foreign_key "comments", "articles"
end

UPDATED: I tried to reproduce the problem in rails console

  • I created a1 article and c1 comment belongs to that article, and when I deleted a1 with .destroy , everything worked fine. Below is my code:
#<Article:0x00007f6d5f437d20
 id: 3,
 title: "My frist article",
 body: "This is my first article",
 created_at: Tue, 04 Jul 2023 01:05:35.148856000 UTC +00:00,
 updated_at: Tue, 04 Jul 2023 01:05:35.148856000 UTC +00:00>

#<Comment:0x00007f6d5f486f88
 id: 6,
 commenter: "Thao",
 body: "This is a comment from Thao",
 article_id: 3,
 created_at: Tue, 04 Jul 2023 01:07:52.288576000 UTC +00:00,
 updated_at: Tue, 04 Jul 2023 01:07:52.288576000 UTC +00:00>


> a1.destroy 
TRANSACTION (0.1ms)  begin transaction
  Comment Load (0.2ms)  SELECT "comments".* FROM "comments" WHERE "comments"."article_id" = ?  [["article_id", 3]]
  Comment Destroy (0.2ms)  DELETE FROM "comments" WHERE "comments"."id" = ?  [["id", 6]]
  Article Destroy (0.2ms)  DELETE FROM "articles" WHERE "articles"."id" = ?  [["id", 3]]
  TRANSACTION (7.2ms)  commit transaction
=> 
#<Article:0x00007f6d5f437d20
 id: 3,
 title: "My frist article",
 body: "This is my first article",
 created_at: Tue, 04 Jul 2023 01:05:35.148856000 UTC +00:00,
 updated_at: Tue, 04 Jul 2023 01:05:35.148856000 UTC +00:00>

UPDATED: I deployed my web app again and the problem is fixed. I thought fly.io would automatically update my code but it's not. Thanks everyone for your help.

  • How did you attempt to delete the article? Add that code to your question. – dbugger Jul 01 '23 at 13:06
  • In your controller, you have to delete your article record with the destroy action. Lets say you have found your article and put it in an environment variable like : `@article`. You have to destroy it like this : `@article.destroy` as the `destroy` method will trigger the dependent callback. If you do `@article.delete` then it will bypass the callbacks (it is a property of `delete`) and then it will leave a comment with an `article_id` that no longer exists. An orphan. And your database may not be happy about that. (this is a just a guess though, please update with your actions if it fails) – Maxence Jul 01 '23 at 21:41
  • @Maxence @dbugger Thank you for your responses. I used `@article.destroy` to delete the article but the problem's still there. Any other suggestions? – Thảo Nguyễn Jul 02 '23 at 01:28
  • Can you please show your database schema (db/schema.rb) in case there is a problem with the migration – Maxence Jul 02 '23 at 23:05
  • @Maxence, I've already added my schema file above, please check it and tell me what you think. Thank you very much for your help – Thảo Nguyễn Jul 03 '23 at 03:28
  • No, you haven't missed anything, there's something else causing the problem. My best guess is that the code you're actually testing hasn't been updated with the `dependent: :destroy` change, whether because of a deploy issue, or a restarting issue. Show us a copy-paste of you reproducing the problem in a new rails console please. – smathy Jul 03 '23 at 13:49
  • @smathy, I updated the code to reproduce the problem in my rails console, please check it out and tell me what you think. Thank you very much. – Thảo Nguyễn Jul 04 '23 at 01:26
  • So it looks like it works just fine? – kwerle Jul 04 '23 at 06:45
  • @kwerle, it works fine in the console but when I open it in web browser, I can not delete the article, I keep receiving the error like I mentioned above – Thảo Nguyễn Jul 04 '23 at 06:58
  • As @smathy and @kwerle suggests, are you sure you have deployed your code with `dependent: :destroy` properly ? – Maxence Jul 04 '23 at 08:17
  • Make some other change to your app, like add some text to the view page that your destroy button/link is on. Then see if that's visible in your deployed app. Assuming it's not, troubleshoot your deploy process. – smathy Jul 04 '23 at 20:54

1 Answers1

0

It is not clear why this is not working in your controller. Looks like maybe some kind of deployment issue. Somehow your code is not in sync.

The solution is clear: pick a test framework and use it. Also, learn to love the debugger.

Write a simple unit test for the model classes involved that show it working at the model level. Should be easy since you've already done it in the console. Then write a request spec that tests at the controller level. It should just work! If it does not then add a debugging breakpoint and dig around.

kwerle
  • 2,225
  • 22
  • 25