我有一个虚拟删除current_user的所有消息的方法,实际上我的意思是它只是将消息表中所有消息的状态设置为1,这意味着消息不会显示给删除消息的用户
message_thread属于message 消息有一个message_thread
这是我的模型方法,它负责这个:
def delete_all_users_messages(user_id, parent_id)
message = Message.find_by_parent_id(parent_id)
message.children.where( :sender_id => user_id ).update_all( :sender_status => 1)
message.children.where( :recipient_id => user_id ).update_all( :recipient_status => 1 )
thread = message.message_thread
thread.update_attribute(:sender_status, 1) if thread.sender_id == user_id
thread.update_attribute(:recipient_status, 1) if thread.recipient_id == user_id
if thread.sender_status == 1 && thread.recipient_status == 1
thread.destroy
Message.destroy_all(:parent_id => parent_id)
end
end
我有一个message_threads表,它引用了我的消息表中的消息。它引用了一个会话的第一条消息,其中parent_id等于消息的id,这意味着。这第一条消息有孩子。
正如您在我的方法结束时所看到的,如果该表中的两个用户状态都设置为1,则会销毁message_threads表中的线程,然后删除所有留下的消息。
我用1个用户帐户对此进行了测试,它将用户message_thread状态更新为1以及所有用户消息。
所以我去了他们正在谈话的用户的帐户并尝试了同样的事情,我得到了:
当我检查记录时,此用户的消息状态确实设置为。消息线程被删除。
但是消息仍然在消息表中,我得到了:
堆叠级别太深
以下是控制器操作:
def destroy_all_messages
Message.delete_all_users_messages(current_user.id, params[:format])
flash[:success] = "Messages deleted"
redirect_to messages_path
end
在我的日志中看起来像一个连续的循环。这可能导致问题吗?我删除了一些,因为它不适合,但我相信你可以看到日志中重复进行。
Started DELETE "/messages/delete_all_messages.315" for 127.0.0.1 at 2012-02-21 15:27:14 +0000
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] Processing by MessagesController#destroy_all_messages as
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] Parameters: {"authenticity_token"=>"yelkcp72223dji4YVumgG9gUEK/U/Mwqwd0pc1WRG+0="}
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] Message Load (0.9ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315 LIMIT 1
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] SQL (20.8ms) UPDATE `messages` SET `sender_status` = 1 WHERE `messages`.`parent_id` = 315 AND `messages`.`sender_id` = 4
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] SQL (37.3ms) UPDATE `messages` SET `recipient_status` = 1 WHERE `messages`.`parent_id` = 315 AND `messages`.`recipient_id` = 4
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] MessageThread Load (0.4ms) SELECT `message_threads`.* FROM `message_threads` WHERE `message_threads`.`message_id` = 315 ORDER BY message_threads.updated_at DESC LIMIT 1
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] (0.1ms) BEGIN
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] (0.3ms) UPDATE `message_threads` SET `recipient_status` = 1, `updated_at` = '2012-02-21 15:27:14' WHERE `message_threads`.`id` = 803
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] (0.4ms) COMMIT
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] (0.1ms) BEGIN
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] SQL (0.2ms) DELETE FROM `message_threads` WHERE `message_threads`.`id` = 803
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] (0.3ms) COMMIT
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] Message Load (0.3ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] (0.1ms) BEGIN
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] CACHE (0.0ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`parent_id` = 315
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] (0.1ms) ROLLBACK
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1] Completed 500 Internal Server Error in 400ms
[51b1090d2a7185c9b2f42fa3213006d1] [127.0.0.1]
SystemStackError (stack level too deep):
actionpack (3.2.0) lib/action_dispatch/middleware/reloader.rb:70
亲切的问候
答案 0 :(得分:2)
我怀疑循环是由destroy
方法引起的。我无法确切地看到您发布的代码的确切方式,但请查找以下任一项:您编写的调用destroy
方法的自定义before_destroy
或destroy
方法其他对象或与:dependent => :destroy[_all]
修饰符的关系。如果这些事情中的任何一个设法创建循环依赖,那么你就陷入了循环。
另外:如果您的模型中destroy
方法不需要执行任何操作,请使用delete
和delete_all
代替该模型。这些不会运行任何析构函数 - 它们只是直接从数据库中删除行 - 因此它们更快,并且您没有冒险循环的风险。当然,当一个模型需要清理它的孩子时,你不能这样做,但对于“叶子”模型,它们很棒。
希望这有帮助!