Questions tagged [dependent-destroy]

27 questions
18
votes
1 answer

What Does Rails Do With Both :dependent => :destroy and cascade delete/nullify/restrict

I'm trying to decide how best to set up (if at all) foreign key constraints for my rails application. I have a model Response that belongs_to a Prompt. I would like to use :dependent => :destroy to have destroy called on every Response that…
Peter Gerdes
  • 2,288
  • 1
  • 20
  • 28
8
votes
1 answer

rails prevent deletion of child unless parent is being deleted also

in Ruby on Rails 4, let's say a parent has many children. When the parent is deleted, the children must also be deleted. Other than that, the child shall not be deleted unless it is an orphan. How to do that? I tried with the following class Parent…
7
votes
6 answers

Add tests for dependent :destroy in the Relationship model (Chapter 11, Exercise 1 Rails Tutorial, 2nd Ed)

Pretty sure these tests are working correctly. Got them to fail by removing the dependent: :destroy options on the has_many :relationships and has_many :reverse_relationships in user.rb. Wanted to share what I did in case anyone else is working…
6
votes
1 answer

How to destroy a record with has_many, :dependent => :destroy

I've built a Rail 3 AuditLog with the help of a few plugins, that store data in an AuditLog Table with the following fields for identification (feeded_id, feeded_type) So in my case, I have a photoalbum that has_many photos. class PhotoAlbum <…
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
6
votes
2 answers

How can I delete an entry from a HABTM join table in rails?

Through many iterations of testing, I just noticed that my join table that represents a HABTM relationship between two models isn't removing entries when instances of these models get deleted. Do I need to do something special when removing an…
5
votes
3 answers

Handle dependent destroy via active jobs

I have a couple models with many children. Dependent destroy has gotten really heavy. Anyone know of a way to tie dependent destroy into active jobs? Or, is my only option to remove dependent destroy and role my own jobs via callbacks on the…
hellion
  • 4,602
  • 6
  • 38
  • 77
2
votes
1 answer

Rails, How to setup a Dependent Destroy for a nested model set?

Possible Duplicate: Rails - Help understanding how to use :dependent => :destroy I have the following models: User Permission (user_id, group_id) Group Conversation (group_id) ConversationParticipation (conversation_id) What I want to do in my…
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
2
votes
3 answers

Rails has_many associations delete parent object but not children

The scenario is that there is two models one is Room and other one is Tickets and the relationship is Room has_many :tickets. The requirement is that when a room is deleting tickets should not be delete. How to accomplish this because tickets table…
2
votes
1 answer

Using dependent: :destroy isn't working on rails

I have a table Bmp with these associations class Bmp < ActiveRecord::Base #associations has_many :subareas, dependent: :destroy belongs_to :scenario and another table Subareas class Subarea < ActiveRecord::Base #associations …
2
votes
0 answers

Rails 3.2.20 - undefined method `name' for nil:NilClass when destroying object

In my project I am using a one-to-many relationship between games and ticker_activites like this Model class Game < ActiveRecord::Base has_many :ticker_activities, :dependent => :destroy, :order => 'time ASC' end class TickerActivity <…
Lars
  • 111
  • 11
2
votes
1 answer

has_one/has_many with dependent destroy but using a different name for the key

So I'm looking at someone's code which has the following (paraphrased): class user has_one :connection, :dependent => :destroy has_one :second_user, :through => :connection, :class_name => 'User' end class connection belongs_to :user …
1
vote
1 answer

Rails association has_one, through, dependent destroy does not destroy related objects

I can't find documentation in the Rails site that covers this particular use case. Presumably normal has_one will work (because it says so). I've not tried yet. Given two models and a join table for the association, I would expect dependent:…
CJBrew
  • 2,720
  • 1
  • 20
  • 27
1
vote
1 answer

:dependent => :destroy doesn't work on has_one relation

In my models class User < ActiveRecord::Base has_one :user_detail, dependent: :destroy end and class UserDetail < ActiveRecord::Base belongs_to :user end When I call destroy for an User object, the associated UserDetail object is not being…
Madalina
  • 457
  • 1
  • 5
  • 14
1
vote
1 answer

rails counter cache while destroy

For example, I have three model user, question and answer, and the relationship between them are: class User < ActiveRecord::Base has_many :answers has_many :questions end class Question < ActiveRecord::Base has_many :answers, :dependent =>…
0
votes
1 answer

How to copy dependant: :destroy variables of one object to another object in Mongoid?

A User class has many devices and deleted_devices. Relationships of User Class: has_many :devices, class_name: "Device", validate: false, autosave: true, dependent: :destroy has_many :deleted_devices, class_name: "DeletedDevice", validate:…
1
2