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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:21:17+00:00 2026-05-30T01:21:17+00:00

We have small class, that has a related class class Car has_one :engine, :dependent

  • 0

We have small class, that has a related class

class Car
  has_one :engine, :dependent => :destroy

  after_update :save_engine


  delegate :oil_level, :oil_level=, :to => :engine

  def save_engine
    engine.save if engine
  end
end

As you can tell, we are trying to work-around the problem that childs are not automatically saved in rails. So we can set the oil_level on car, and it will store it in engine correctly when saving.

Now what happens when we try to delete the car? Apparently the engine is never deleted, and this is caused by the after_update which will re-save the just deleted engine. So the car is succesfully deleted, but the engine is recreated (a new id), and no longer linked to a car. I want the engine to be deleted correctly.

I did find a solid work-around:

class Car
  has_one :engine, :dependent => :destroy

  after_update :save_engine
  before_destroy :set_destroying


  delegate :oil_level, :oil_level=, :to => :engine

  def set_destroying
    @destroying = true
  end

  def save_engine
    engine.save if engine && !@destroying
  end
end

But it still feels a bit dirty, like there should be a better way to do this.
It seems a bit unlogical that after_update is called when destroying.
Is there a specific rails-way to know what action is actually in progress in a callback (saving/creating/destroying) ?.

In short: what is the proper way to handle this? Or is this actually some kind of rails-bug? Or is this intended behaviour?

  • 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-30T01:21:19+00:00Added an answer on May 30, 2026 at 1:21 am

    Haven’t done Rails for a while, so I may be off topic, but have you considered using the :autosave option?

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

Sidebar

Related Questions

I have a small class that displays an alert via Javascript. I am using
I have this small class called City that simply holds some information about a
I have a not-so-small class under development (that it changes often) and I need
I have a class which has many small functions. By small functions, I mean
I have an interface that has multiple small JTables stacked on top of each
I have a class that has a vector of another class objects as a
I have a small class hierarchy that I'm having trouble implementing copyWithZone: for. I've
I have written a small wrapper for the dict built-in class that loads entries
OK, so I have a database field of type char(1) that has a small
I have a Java class that has access to a transaction context that I'd

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.