LondonGuy
LondonGuy

Reputation: 11098

Why are associated records being deleted when dependant destroy is not set in ruby on rails?

For some reason when I delete an uploaded photo, the album it belongs to is automatically deleted too.

Makes no sense seeing as I haven't set "dependant destroy" on any model.

Here is the terminal logs:

Started DELETE "/settings/photo_gallery/photos/86" for 127.0.0.1 at 2012-08-28 22:39:50 +0100
[af820895e0bf5c0b031bb5ea297290ee] [127.0.0.1] Processing by PhotosController#destroy as JS
[af820895e0bf5c0b031bb5ea297290ee] [127.0.0.1]   Parameters: {"id"=>"86"}
[af820895e0bf5c0b031bb5ea297290ee] [127.0.0.1]   Photo Load (0.4ms)  SELECT `photos`.* FROM `photos` WHERE `photos`.`id` = 86 LIMIT 1
[af820895e0bf5c0b031bb5ea297290ee] [127.0.0.1]   PhotoAlbum Load (0.2ms)  SELECT `photo_albums`.* FROM `photo_albums` WHERE `photo_albums`.`photo_id` = 86 LIMIT 1
[af820895e0bf5c0b031bb5ea297290ee] [127.0.0.1]   Profile Load (3.2ms)  SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`photo_id` = 86 LIMIT 1
[af820895e0bf5c0b031bb5ea297290ee] [127.0.0.1]    (0.3ms)  BEGIN
[af820895e0bf5c0b031bb5ea297290ee] [127.0.0.1]   SQL (0.6ms)  DELETE FROM `photos` WHERE `photos`.`id` = 86
[af820895e0bf5c0b031bb5ea297290ee] [127.0.0.1]    (12.0ms)  COMMIT
[af820895e0bf5c0b031bb5ea297290ee] [127.0.0.1]   PhotoAlbum Load (15.2ms)  SELECT `photo_albums`.* FROM `photo_albums` WHERE `photo_albums`.`id` = 36 LIMIT 1
[af820895e0bf5c0b031bb5ea297290ee] [127.0.0.1] Redirected to http://localhost:3000/settings/photo_gallery/36
[af820895e0bf5c0b031bb5ea297290ee] [127.0.0.1] Completed 302 Found in 46ms (ActiveRecord: 31.9ms)
[affa94ee1fa7dde2725d6f75c8cfb998] [127.0.0.1] 

Started DELETE "/settings/photo_gallery/36" for 127.0.0.1 at 2012-08-28 22:39:50 +0100
[affa94ee1fa7dde2725d6f75c8cfb998] [127.0.0.1] Processing by PhotoAlbumsController#destroy as JS
[affa94ee1fa7dde2725d6f75c8cfb998] [127.0.0.1]   Parameters: {"id"=>"36"}
[affa94ee1fa7dde2725d6f75c8cfb998] [127.0.0.1]   PhotoAlbum Load (1.2ms)  SELECT `photo_albums`.* FROM `photo_albums` WHERE `photo_albums`.`id` = 36 LIMIT 1
[affa94ee1fa7dde2725d6f75c8cfb998] [127.0.0.1]    (0.1ms)  BEGIN
[affa94ee1fa7dde2725d6f75c8cfb998] [127.0.0.1]   SQL (0.3ms)  DELETE FROM `photo_albums` WHERE `photo_albums`.`id` = 36
[affa94ee1fa7dde2725d6f75c8cfb998] [127.0.0.1]    (4.6ms)  COMMIT
[affa94ee1fa7dde2725d6f75c8cfb998] [127.0.0.1] Redirected to http://localhost:3000/settings/photo_gallery
[affa94ee1fa7dde2725d6f75c8cfb998] [127.0.0.1] Completed 302 Found in 22ms (ActiveRecord: 6.3ms)
[e5d66fa562a52440eb4e710bfc660402] [127.0.0.1] 

Photo album model:

class PhotoAlbum < ActiveRecord::Base

    belongs_to :user
    has_many :photos
    belongs_to :photo

    attr_accessible :album_title, :user_id, :photo_id

Photo model:

class Photo < ActiveRecord::Base


    belongs_to :photo_album


    attr_accessible :photo_album_id, :photo_title, :image, :remote_image_url, :banner_axis
    mount_uploader :image, ImageUploader

Users show view:

<%= link_to "", @banner, :confirm => 'Are you sure?', :method => :delete, :remote => true %>

Users controller show action:

 def show
   profile_banner_album_id = @user.photo_albums.find_by_album_title("profile banner").id
   @banner = Photo.find_by_photo_album_id(profile_banner_album_id)
 end

I've been away from rails for a while so slightly rusty so I may be missing something quite obvious. Looking at the logs I'm not sure why it goes to delete the photo album after the photo has been deleted.

Looking forward to some help here. Kind regards

Upvotes: 0

Views: 86

Answers (2)

Intrepidd
Intrepidd

Reputation: 20858

Started DELETE "/settings/photo_gallery/36" for 127.0.0.1 at 2012-08-28 22:39:50 +0100
[affa94ee1fa7dde2725d6f75c8cfb998] [127.0.0.1] Processing by PhotoAlbumsController#destroy as JS

This means that the destroy action is called on your album controller via JS.

This is not a rails issue, you have another JS call that destroys the album after deleting the photo.

Maybe when you click on the delete button for the photo, the delete button of the album is clicked as well, or something like that ...

Upvotes: 1

TomDunning
TomDunning

Reputation: 4877

This line:

[af820895e0bf5c0b031bb5ea297290ee] [127.0.0.1] Completed 302 Found in 46ms (ActiveRecord: 31.9ms)

Would suggest that the first action is being completed and a second action is being called at the same time. There's nothing strange in photo_gallery#index is there? Or an filter on the photo model?

Upvotes: 1

Related Questions