Skip to content

STI subclass with model_name causes warning #654

@digitalcora

Description

@digitalcora

This is admittedly a bit of an unusual situation, and (for reasons I don't yet understand) appears to still work as intended, so I'm mostly just documenting it here to provide a public reference point.

Consider the following models and resources:

class Machine < ActiveRecord::Base
  has_many :parts
end

class Part < ActiveRecord::Base
  belongs_to :machine
end

class Gadget < Part
  # ...
end

class PartResource < JSONAPI::Resource
  has_one :machine
end

class WidgetResource < PartResource
  model_name 'Gadget'
end

On definition of the WidgetResource class, JR issues the following warning:

[MODEL NOT FOUND] Model could not be found for WidgetResource. If this a base Resource declare it as abstract.

This is caused by the inherited hook in JSONAPI::Resource, which ends up calling _model_class, and since this happens before model_name is called, the model class cannot be found. Since the call is related to associations, a workaround is to define the has_one :machine on each subclass instead of the superclass. Although, as I mentioned, even with the warning the association still seems to work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions