1

Here's the show.html.arb file:

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

And here's the ActiveAdmin.register Request part:

show do |resource|
  render 'show'
end

Here's the link that is used to render this page:

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

My question then is, why on earth does this tell me it's trying to remove the association??

This is a GET request, not a PUT or POST request.

Here's the exact error:

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

It is, indeed, removing the association when I simply view the show page for this record. The @2nd column is the association's id

Nick Res
  • 2,154
  • 5
  • 30
  • 48

1 Answers1

0

The question doesn't show this - but one of the models as an after_initialize callback declared within it.

The logs tell me that this method is being called here where it says 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'

Now, here's what the documentation says about this callback:

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.

This means that just by doing ar_object_thingy.associated_thingy it's calling the after_initialize callback. Within that callback I'm doing self.build_thingy (example) to create an object within a polymorphic relationship. This is actually destroying the current relationship that's there with an un-persisted object which is causing the error.

TLDR;

The issue isn't with ActiveRecord in this case. It's with a model callback I'm using called after initialize that is calling a method which breaks the belongs_to relationship this model validates the presence of every time I simply look up the parent record's child through the parent.

Nick Res
  • 2,154
  • 5
  • 30
  • 48