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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:09:39+00:00 2026-06-06T22:09:39+00:00

I’m trying to prevent an associated record from being deleted if it has associated

  • 0

I’m trying to prevent an associated record from being deleted if it has associated records of its own. Rails seems to be subverting the :dependent => :restrict option and executing DELETE SQL anyway. What am I missing?

class User < ActiveRecord::Base
  has_many :user_regions
  has_many :events, :through => :user_regions
  has_many :regions, :through => :user_regions
end

class Region < ActiveRecord::Base
  has_many :user_regions
  has_many :events, :through => :user_regions
end

class UserRegion < ActiveRecord::Base
  belongs_to :user
  belongs_to :region

  has_many :events, :dependent => :restrict
end

class Event < ActiveRecord::Base
  belongs_to :user_region, :readonly => true
  has_one :planner, :through => :user_region, :source => :user
  has_one :region, :through => :user_region
end

Now assume I have two regions [R1 and R2], two users [U1 and U2]. U1 has been assigned to both R1 and R2 [as UR1 and UR2], and U2 has been assigned only to R2 [as UR3]. This gives a total of three UserRegions.

Additionally, I have events E1, E2, and E3, where E1 is in UserRegion UR1, E2 is in UserRegion UR2 and E3 is in UserRegion UR3. This gives U1 two events and U2 one event.

WHAT SHOULD HAPPEN:
If I attempt to remove U1’s assignment to R2 (i.e. destroy UR2), I should get an error, as U1 has events through UR2.

WHAT IS HAPPENING:

u1 = User.find(1)
urids = u1.regions.map &:id
 => [1,2]
u1.region_ids = [1]
Region Load (0.3ms)  SELECT "regions".* FROM "regions" WHERE "regions"."id" = ? LIMIT 1  [["id", 1]]
Region Load (0.3ms)  SELECT "regions".* FROM "regions" INNER JOIN "user_regions" ON "regions"."id" = "user_regions"."region_id" WHERE "user_regions"."user_id" = 1
 (0.1ms)  begin transaction
SQL (0.3ms)  DELETE FROM "user_regions" WHERE "user_regions"."user_id" = 1 AND "user_regions"."region_id" = 2
 (2.7ms)  commit transaction
 => [1] 

Obviously Rails is not executing the :destroy callbacks on the UserRegion being deleted, because if I attempted to do:

UserRegion.find(2).destroy

I would get an error and the transaction would rollback.

What do I need to do to ensure that assigning Region id’s to User doesn’t delete UserRegions?

The goal here is that the “edit user” form has a list of check boxes for assigning regions. It should be possible to add regions to a user and remove regions through which the user has no events.

  • 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-06T22:09:41+00:00Added an answer on June 6, 2026 at 10:09 pm

    From the ActiveRecord::Associations documentation, in the ‘Delete or Destroy?’ section:

    For has_many, destroy will always call the destroy method of the
    record(s) being removed so that callbacks are run. However delete will
    either do the deletion according to the strategy specified by the
    :dependent option, or if no :dependent option is given, then it will
    follow the default strategy. The default strategy is :nullify (set the
    foreign keys to nil), except for has_many :through, where the default
    strategy is delete_all (delete the join records, without running their
    callbacks).

    So in my User model above, I needed to change this:

    class User < ActiveRecord::Base
      has_many :user_regions
      has_many :events, :through => :user_regions
      has_many :regions, :through => :user_regions
    end
    

    into this:

    class User < ActiveRecord::Base
      has_many :user_regions
      has_many :events, :through => :user_regions
      has_many :regions, :through => :user_regions, :dependent => :destroy
    end
    

    This effectively prevents a UserRegion from being deleted if the User still has Events through the UserRegion. An ActiveRecord::DeleteRestrictionError will be raised if this is attempted.

    Handling this error at the controller level and setting up my view so this condition is never encountered is, of course, another story.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.