Prev | Current Page 111 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

destroy
it will result in the following SQL statements being executed:
SELECT * FROM people WHERE (people.parent_id = 1)
BEGIN
SELECT * FROM people WHERE (people.parent_id = 2)
DELETE FROM people WHERE `id` = 2
SELECT * FROM people WHERE (people.parent_id = 3)
DELETE FROM people WHERE `id` = 3
DELETE FROM people WHERE `id` = 1
COMMIT
As you can see in these SQL statements, each of the children is loaded and deleted, and
then the parent object is deleted as well. Each of the objects is loaded because each is literally
having destroy called on it, which means that all of the callbacks are called as with a normal
destroy call. If you do not wish for all of the callbacks on the children to be called, you can
change :dependent => :destroy to :dependent => :delete_all. If you do this, the call to
bm.destroy will result in the following SQL statements being called:
BEGIN
DELETE FROM people WHERE (parent_id = 1)
DELETE FROM people WHERE `id` = 1
COMMIT
You can see from this SQL that when :dependent => :delete_all is used on the children??™s
relationship, the children are not loaded.


Pages:
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123