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 8620433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:34:43+00:00 2026-06-12T06:34:43+00:00

I have a Writer model that has_many books . Each book has_many orders .

  • 0

I have a Writer model that has_many books. Each book has_many orders. In order to prevent unwanted deletions, the Book model has a before_destroy hook method to make sure it isn’t referenced in the Order model, as suggested in PragProg Agile Web Dev (p.7)

Book.rb

before_destroy: :ensure_not_referenced_by_any_order

def ensure_not_referenced_by_any_order
  if orders.empty?
    return true
  else
    return false
  end
end

How would I write a before_destroy hook method for the Writer model so that for Writer.books.each, no orders exist? I started writing the below, but can’t figure out exactly how to write it.

Writer.rb

before_destroy: :ensure_books_not_referenced_by_any_order

def ensure_books_not_referenced_by_any_order
  self.books.each do |book|
    if book.orders.empty?
      return true
    else
      return false
    end
  end
end

Thanks very much for your help!

  • 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-06-12T06:34:45+00:00Added an answer on June 12, 2026 at 6:34 am

    Couldn’t get @Mark’s suggestion to work. Resorted to restricting only for books.empty?, meaning a writer must delete all of his books first in order to delete his own profile.

    def ensure_has_no_books
      if books.empty?
        return true
      else
        return false
      end
    end
    

    Please let me know if you have insight on how I can get the has_no_books_reference_by_any_order loop to work, and I will accept if it does. Thanks!

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

Sidebar

Related Questions

I have a User model that has many fights. Fight belongs to User. There
I have a Rails app with an Order and a Refund model. Order has_many
I have a User model and the user has a relationship which is has_many
I have two data models: Writer.AttributValeur and Writer.Produit . Writer.Produit has HasMany / BelongsTo
I'm writing an application that has a multiple producer, single consumer model (multiple threads
I have a Post model: class Post < ActiveRecord::Base belongs_to :user has_many :taggings, :dependent
I have a Tag model: class Tag < ActiveRecord::Base has_many :taggings, :dependent => :destroy
I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names has_many
I have an application that has many views (Using tab bar controller). I want
I have a Java Swing application, that has many JTextFields and a datamodel. When

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.