Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6755783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:26:39+00:00 2026-05-26T13:26:39+00:00

So I have a model, let’s call it Notes. On the notes, you can

  • 0

So I have a model, let’s call it Notes. On the notes, you can but several posts. So the notes model has the line:

 has_many :posts

And the posts model has the line

 belongs_to :note

Now, when a User destroys a post, I want for the note to be destroyed IF it not longer has any other posts.

I thought i would write this code into the post model with before_destroy:

 def delete_note_if_last_post
    if self.note.posts.count == 1
      self.note.destroy
    end
  end

This doesn’t work. It shuts down the server based on an “Illegal Instruction”. Is there some way to accomplish what I am trying to do?

EDIT: changed the code, as I noticed an error, and now the problem is slightly different.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:26:39+00:00Added an answer on May 26, 2026 at 1:26 pm

    you can return false to prevent a model from destruction in before_destroy filter

    before_destroy :has_no_post
    

    then in has_no_post

    def has_no_post
     #You can prevent this from deletion by using these options
     #Option1 return false on certain condition  
    return false if posts.any?
    
    #or add an error to errors
    errors << "Can not delete note if it has post" if posts.any?
    #raise an exception 
    raise "Cant delete ..." if blah blah
    

    end

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model with has_many association. Let's just say Student has many Courses.
Let's say I have a model called Theme, which has several attributes setting interface
In a webapp, i have a data-model (let's call it Item). The data of
Let's assume I have a model called product. Let's assume that product has three
Let's say I have a model class called Project, but instead of this: class
Let's say I have a model Vehicle and it has_many :wheels What is the
Let's say I have a model called Topic. A Topic has_many :topic_flags (a TopicFlag
Let's say I have a model foo, and my model has a publish! method
Assume, we have a model, let's call it Articles. There, we have two columns,
Let's say I have to model the meals of a diner. A meal can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.