代码之家  ›  专栏  ›  技术社区  ›  Nick Res

ActiveAdmin在尝试呈现show页面时删除关联

  •  1
  • Nick Res  · 技术社区  · 9 年前

    这是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。注册请求部分:

    show do |resource|
      render 'show'
    end
    

    <%= content_tag(:li, link_to('View', accounting_request_path(accnt_request))) %>
    

    那么,我的问题是,这到底为什么告诉我它试图消除这种联系??

    下面是确切的错误:

    Failed to remove the existing associated request. The record failed to save when after its foreign key was set to nil.
    

    事实上,当我只是查看这个记录的显示页面时,就删除了关联。 The @2nd column is the association's id

    1 回复  |  直到 9 年前
        1
  •  0
  •   Nick Res    9 年前

    问题并没有说明这一点,而是将其中一个模型作为 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'
    

    现在,以下是文档中关于此回调的内容:

    最后,为触发after\u find and after\u initialize回调 具有 好

    这意味着 ar_object_thingy.associated_thingy 它正在呼叫 初始化后 self.build_thingy (示例)在多态关系中创建对象。这实际上破坏了当前与导致错误的未持久化对象的关系。

    在本例中,问题不在于ActiveRecord。这是我使用的一个模型回调,名为 after initialize 这就是调用一个方法,该方法打破了归属关系。每当我通过父记录查找父记录的子记录时,该模型都会验证是否存在。