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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:22:21+00:00 2026-05-31T22:22:21+00:00

I have the following model associations: class Slider < ActiveRecord::Base has_one :featured_happening, :as =>

  • 0

I have the following model associations:

class Slider < ActiveRecord::Base
  has_one :featured_happening, :as => :featured_item, :dependent => :destroy   
  before_destroy :destroy_featured_happening
  after_create :create_featured_happening
end

class FeaturedHappening < ActiveRecord::Base
  belongs_to :featured_item, :polymorphic => true
end

When I destroy a Slider object I thought the dependent => :destroy would automatically destroy the featured_item but it does not.

Slider controller

 def destroy    
    slider = Slider.find(params[:id])
    slider.delete
    render(:status => :ok, :nothing => true )
  end

So then I tried a callback with before_destroy to manually delete the featured_item when a slider object is destroyed and nothing is getting called.

How can I get the featured_item to be deleted when I delete a slider object? Using Rails 3.2.

  • 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-31T22:22:22+00:00Added an answer on May 31, 2026 at 10:22 pm

    You just observed the difference between delete and destroy. In your controller you call

    slider.delete
    

    which will just execute a SQL DELETE but will not call any callbacks. That’s why in normal cases you want to use destroy instead. It will fetch the object (if necessary), call the callbacks including recursive destroys and only then deletes the object from the database.

    See the documentation for delete and destroy for more information.

    • 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 the following associations: class Car < ActiveRecord::Base belongs_to :transportation_vehicle
I have the following model associations: class Product < ActiveRecord::Base has_many :prices class Price
I have the following models, with their relevant associations: class User < ActiveRecord::Base has_many
I have the following :has_many :through relation. Associations class Profile < ActiveRecord::Base has_many :teams
I have model: class Topic < ActiveRecord::Base attr_accessible :title, :description has_many :comments, :dependent =>
I have three classes with the following associations: class Technician < ActiveRecord::Base has_many :tickets
I have the following models with associations as given below:- class Comment < ActiveRecord::Base
My system has the following associations set up: class Book < ActiveRecord::Base has_many :
I have got the following models, class City < ActiveRecord::Base belongs_to :state belongs_to :country
I have the following models: class Aircon < ActiveRecord::Base belongs_to :shop belongs_to :brand belongs_to

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.