ActiveAdmin 仅在尝试呈现显示页面时删除关联
ActiveAdmin deletes associations when simply trying to render show page
这是 show.html.arb 文件:
attributes_table_for resource do
row :id
row :state
row :request_type
row :assignee
row :assigner
row :unassigned
row :deleter
end
attributes_table_for resource.request do
row :id
end
这里是 ActiveAdmin.register 请求部分:
show do |resource|
render 'show'
end
这是用于呈现此页面的 link:
<%= content_tag(:li, link_to('View', accounting_request_path(accnt_request))) %>
那么我的问题是,为什么这告诉我它正在尝试删除关联?
这是 GET 请求,不是 PUT 或 POST 请求。
这是确切的错误:
Failed to remove the existing associated request. The record failed to save when after its foreign key was set to nil.
当我只是查看此记录的显示页面时,确实是删除了关联。
问题没有显示这一点 - 但其中一个模型作为 after_initialize
回调在其中声明。
日志告诉我这里调用了这个方法,上面写着 build_requestable
:
app/models/concerns/requestable.rb:6:in `build_requestable'
app/views/accounting_requests/_show.html.arb:6:in `block in _app_views_accounting_requests__show_html_arb___512970926778810589_70108054037800'
app/views/accounting_requests/_show.html.arb:1:in `new'
app/views/accounting_requests/_show.html.arb:1:in `_app_views_accounting_requests__show_html_arb___512970926778810589_70108054037800'
app/ui/stewgle/tasks/accounting_requests.rb:33:in `block (2 levels) in <top (required)>'
lib/monkies/aa_views_pages_base.rb:62:in `block in build_page_content'
lib/monkies/aa_views_pages_base.rb:60:in `build_page_content'
app/controllers/application_controller.rb:57:in `set_time_zone'
现在,这是文档中关于此回调的内容:
Lastly an after_find and after_initialize callback is triggered for
each object that is found and instantiated by a finder, with
after_initialize being triggered after new objects are instantiated as
well.
这意味着只要执行 ar_object_thingy.associated_thingy
就会调用 after_initialize
回调。在该回调中,我正在执行 self.build_thingy
(示例)以在多态关系中创建一个对象。这实际上是在破坏当前与导致错误的非持久化对象之间的关系。
TLDR;
在这种情况下,问题不在于 ActiveRecord。它与我正在使用的称为 after initialize
的模型回调一起调用了一种方法,该方法打破了 belongs_to 关系,每次我只是通过父记录查找父记录的子记录时,该模型都会验证是否存在。
这是 show.html.arb 文件:
attributes_table_for resource do
row :id
row :state
row :request_type
row :assignee
row :assigner
row :unassigned
row :deleter
end
attributes_table_for resource.request do
row :id
end
这里是 ActiveAdmin.register 请求部分:
show do |resource|
render 'show'
end
这是用于呈现此页面的 link:
<%= content_tag(:li, link_to('View', accounting_request_path(accnt_request))) %>
那么我的问题是,为什么这告诉我它正在尝试删除关联?
这是 GET 请求,不是 PUT 或 POST 请求。
这是确切的错误:
Failed to remove the existing associated request. The record failed to save when after its foreign key was set to nil.
当我只是查看此记录的显示页面时,确实是删除了关联。
问题没有显示这一点 - 但其中一个模型作为 after_initialize
回调在其中声明。
日志告诉我这里调用了这个方法,上面写着 build_requestable
:
app/models/concerns/requestable.rb:6:in `build_requestable'
app/views/accounting_requests/_show.html.arb:6:in `block in _app_views_accounting_requests__show_html_arb___512970926778810589_70108054037800'
app/views/accounting_requests/_show.html.arb:1:in `new'
app/views/accounting_requests/_show.html.arb:1:in `_app_views_accounting_requests__show_html_arb___512970926778810589_70108054037800'
app/ui/stewgle/tasks/accounting_requests.rb:33:in `block (2 levels) in <top (required)>'
lib/monkies/aa_views_pages_base.rb:62:in `block in build_page_content'
lib/monkies/aa_views_pages_base.rb:60:in `build_page_content'
app/controllers/application_controller.rb:57:in `set_time_zone'
现在,这是文档中关于此回调的内容:
Lastly an after_find and after_initialize callback is triggered for each object that is found and instantiated by a finder, with after_initialize being triggered after new objects are instantiated as well.
这意味着只要执行 ar_object_thingy.associated_thingy
就会调用 after_initialize
回调。在该回调中,我正在执行 self.build_thingy
(示例)以在多态关系中创建一个对象。这实际上是在破坏当前与导致错误的非持久化对象之间的关系。
TLDR;
在这种情况下,问题不在于 ActiveRecord。它与我正在使用的称为 after initialize
的模型回调一起调用了一种方法,该方法打破了 belongs_to 关系,每次我只是通过父记录查找父记录的子记录时,该模型都会验证是否存在。