Issues with rails blogger assignment (tags)

I’m at an utter loss here. In my tags controller, the method I have for destroy is (virtually the same as the articles one):
def destroy
@tag = Tag.find(params[:id])

flash.notice = "Tag '#{}' Deleted!"

redirect_to tag_path


However I get an error when I actually attempt to destroy a tag, stating: SQLite3::ConstraintException: FOREIGN KEY constraint failed: DELETE FROM “tags” WHERE “tags”.“id” = ?

I’m not quite sure where this foreign key issue is originating, nor how to fix it. Any help would be greatly appreciated.

1 Like

Hey @markb

In your tags model the line for has_many taggings needs to be changed to

has_many :taggings, dependent: :destroy

I think that’s correct but I’m on my phone so can’t be 100% sure. Essentially when you delete a tag, Rails wants to force the deletion of any associated tags in taggings, otherwise if you delete a tag then you could still have taggings that reference tags that don’t exist. It wasn’t always this way so the tutorial is a little out of date and will hopefully be updated shortly.


Thank you - that fixed the current error message, but now I get ‘Couldn’t find Tag with ‘id’=[whatever id # the tag currently is]’

It is in fact deleting the tags now, but I am receiving the error upon rails calling the tag controller show method. It’s trying to show a tag that, once deleted, no longer exists. I can’t for the life of me figure out why it’s trying to show tags that no longer exist. Shouldn’t @tag.articles.each do etc… not have that tag in its array?

Is the controller trying to route back to the tag after deleting it?

1 Like

You sir, are a genius. Thank you so much. That was absolutely it. @CouchofTomato

I ran into this same issue. Thank you both!

Also had this problem! Posting so that others can see that there IS a solution. Fixed my issues for both Article deletion and Tag deletion.

I was banging my head against the wall a bit until I found this answer! Thank you :slight_smile: