如何正确销毁 ruby on Rails 中的关联记录?
我有一种方法实际上可以删除 current_user 的所有消息,实际上我的意思是它只是将消息表中所有消息的状态设置为 1,这意味着该消息不会向删除该消息的用户显示。
message_thread属于消息 消息有一个 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
亲切的问候
I have a method that virtually deletes ALL messages of a current_user and by virtually I mean it just sets the status for all there messages in the message table to 1 which means the message won't show to the user who deletes the messages.
message_thread belongs to message
message has one message_thread
Here is my model method that takes care of this:
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
I have a message_threads table that references messages in my messages table. it references the first message of a conversation who's parent_id is equal to the id of the message which means. This first message has children.
As you can see at the end of my method I destroy the thread in the message_threads table if both users status's in that table are set to 1 then delete any messages left.
I tested this with 1 users account and it worked for updating the users message_thread status to 1 and all that users messages.
So I went to the account of the user they were in conversation with and tried the same thing and I get:
When I check the records the messages status's for this user do get set to. The message thread is deleted.
But the messages are still in the message table and I get:
stack level too deep
Here is the controller action:
def destroy_all_messages
Message.delete_all_users_messages(current_user.id, params[:format])
flash[:success] = "Messages deleted"
redirect_to messages_path
end
Looks like a continuous loop in my logs. Is this what could be causing the issue? I deleted some because it wouldn't all fit but I'm sure you can see the repeating going on in the logs.
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
Kind regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑循环是由
destroy
方法引起的。我无法从您发布的代码中确切地看出如何进行操作,但请查找以下任一内容:您编写的调用调用的自定义destroy
或before_destroy
方法其他对象或与:dependent => 的关系上的
修饰符。如果其中任何一个因素设法创建循环依赖关系,那么您就会陷入循环。destroy
方法:destroy[_all]另外:如果您的模型的
destroy
方法不需要执行任何操作,请对该模型使用delete
和delete_all
。它们不运行任何析构函数 - 它们只是直接从数据库中删除行 - 因此它们速度更快,并且您不会冒循环的风险。当然,当模型需要清理它的子模型时,你不能这样做,但对于“叶子”模型来说,它们非常棒。希望这有帮助!
I suspect the loop is being caused by the
destroy
methods. I can't see exactly how from the code you've posted, but look for either of the following: Customdestroy
orbefore_destroy
methods that you've written that call thedestroy
method on other objects, or relations with a:dependent => :destroy[_all]
modifier. If either of those things manage to create a cyclic dependency, you're stuck in a loop.Also: If you have a model where the
destroy
method doesn't need to do anything, usedelete
anddelete_all
for that model instead. These don't run any destructors - they just delete rows straight from the DB - so they're faster, and you don't risk looping. Of course, you can't do this when a model is expected to clean up it's children, but for "leaf" models, they're great.Hope this helps!