activeadmin: Namespaced "X::Y" resource belongs_to, raises TargetNotFound exception

A namespaced belongs_to resource raises TargetNotFound exception.

class Tracking::Visit < ActiveRecord::Base
  has_many :page_views, :class_name => "Tracking::PageView", :dependent => :destroy
end

class Tracking::PageView < ActiveRecord::Base
  belongs_to :visit, :class_name => "Tracking::Visit"
end

ActiveAdmin.register Tracking::Visit do
end

ActiveAdmin.register Tracking::PageView do
  belongs_to :tracking_visit, :parent_class=>Tracking::Visit #I think I've added parent_class for it to work on 3.4.x
end

The exception is raised here: ActiveAdmin::Resource::BelongsTo

# Returns the target resource class or raises an exception if it doesn't exist
      def target
        namespace.resources.find_by_key(@target_name.to_s.camelize) or 
          raise TargetNotFound, "Could not find registered resource #{@target_name} in #{namespace.name} with #{namespace.resources.inspect}"
      end

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 23 (12 by maintainers)

Most upvoted comments

The issue appears to be that active admin executes resource associations during file loading. This means that if your belongs_to file is before your parent file alphabetically, it won’t be able to find it.

eg

bad

# a.rb
ActiveAdmin.register A do
  belong_to :b # will not be able to locate `b` until `b.rb` is loaded.
end
# b.rb
ActiveAdmin.register B do
...
end

good (my workaround)

# b_a.rb << nested naming
ActiveAdmin.register A do
  belong_to :b
end
# b.rb
ActiveAdmin.register B do
...
end

ActiveAdmin v 1.4.3

Took me the good part of a day to figure this out. Now stuck on some other random error

I just ran in to this as well - using whatever version master is as of the date of this comment