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

  • Home
  • SEARCH
  • 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 7762643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:27:35+00:00 2026-06-01T14:27:35+00:00

class Assembly < ActiveRecord::Base has_and_belongs_to_many :parts end class Part < ActiveRecord::Base has_and_belongs_to_many :assemblies end

  • 0
class Assembly < ActiveRecord::Base
  has_and_belongs_to_many :parts
end

class Part < ActiveRecord::Base
  has_and_belongs_to_many :assemblies
end

In console:

part1 = Part.new
assembly1 = Assembly.new
assembly1.parts << part1
part1.delete
Parts.all
 => []

Checking assembly1.parts shows that there is still a relationship.(!)

How is this possible when the record was deleted?

Also, how to prevent deletion of parts that are associated to assemblies?

Working in Rails 3.0.7.

  • 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-01T14:27:37+00:00Added an answer on June 1, 2026 at 2:27 pm

    Everything you were doing here was done from memory (nothing was stored in the database).

    the ActiveRecord delete method will remove an object from the database but it doesn’t look for other objects in memory that may have already been referencing that object. I think if you did assembly1.parts.delete(part1) that would likely do what you were expecting.

    If you had saved the objects to the database:

    part1 = Part.create
    assembly1 = Assembly.create(:parts => [part1])
    assembly1.parts
    # => [part1]
    part1.delete
    assembly1.parts
    # => [part1]
    assembly1.reload
    assembly1.parts
    # => []
    

    Note here how even if it’s in the database part1.delete won’t necessarily remove it from your assembly object until you refresh the in-memory collection or delete it using the method I mentioned earlier assembly1.parts.delete(part1)

    UPDATE

    I think you usually shouldn’t use the delete() method. You should almost always use destroy(). delete() will just fire off a delete to the database and ignores all callbacks and I believe :dependent => :destroy-style declarations in your model. If you use the destroy() method then you can declare a before_destroy callback in your model:

    class MyClass
      has_and_belongs_to_many :foos
    
      before_destroy :allow_destroy
    
      def allow_destroy
        foos.empty?
      end
    end
    

    That should get your requirement of not destroying it if it is part of an assembly. You cannot stop delete() from executing because it ignores callbacks: ActiveRecord::Relation#delete documentation

    More info about model callbacks (documentation)

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

Sidebar

Related Questions

I'm in an Assembly class focusing on the intel 8086 architecture (all compiling /
Say I have a base class in Assembly A: public class MyBaseClass{ public static
Is it possible to mock the Assembly class? If so, using what framework, and
Is a .NET assembly loaded by the CLR when a class from the assembly
In an assembly I created a class like the following: [DataObject(true)] public class A
I have a class that is in an assembly due to being required for
I have a third party assembly with a public abstract class implementing a certain
I am currently trying to profile a class contained in a different assembly. To
I want to merge one .NET DLL assembly and one C# Class Library project
My custom tool is as follows: Class: FooBar.Blah.Tools.DataContextClassGenerator Assembly: FooBar.Blah.Tools.DataContextClassGenerator Project: FooBar.Blah.Tools.DataContextClassGenerator The CreatePkgDef

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.