为什么在rails上的ruby中没有设置依赖destroy时删除关联的记录?

时间:2012-08-28 22:16:15

标签: ruby-on-rails ruby ruby-on-rails-3 rubygems ruby-on-rails-3.2

出于某种原因,当我删除上传的照片时,它所属的相册也会自动删除。

没有任何意义,因为我没有在任何模型上设置“依赖性破坏”。

以下是终端日志:

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] 

相册模型:

class PhotoAlbum < ActiveRecord::Base

    belongs_to :user
    has_many :photos
    belongs_to :photo

    attr_accessible :album_title, :user_id, :photo_id

照片模型:

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

用户显示视图:

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

用户控制器显示操作:

 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

我已经远离铁轨一段时间了,所以我可能会遗漏一些非常明显的东西。查看日志我不确定为什么在删除照片后删除相册。

期待在这里提供一些帮助。 亲切的问候

2 个答案:

答案 0 :(得分:1)

这一行:

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

建议第一个操作正在完成,同时调用第二个操作。 photo_gallery#index中有什么奇怪的吗? 或照片模型上的过滤器?

答案 1 :(得分: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

这意味着通过JS在您的专辑控制器上调用destroy动作。

这不是铁轨问题,你有另一个JS电话,在删除照片后会破坏相册。

也许当您点击照片的删除按钮时,也会点击相册的删除按钮,或类似的内容......