Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

activerecord - Rails 6 API Only - Namespace Models - Can not resolve the namespace models in controllers

I have namespace models like so

# /app/models/metadata.rb
module Metadata
  class << self
    def table_name_prefix
      "metadata_"
    end
  end
end
# /app/models/metadata/object.rb
class Metadata::Object < ActiveRecord::Base
end
# no nested
class TestController < ActionController::API
end 

When I run in rails c it can resolve the Metadata::Object like so

Metadata::Object.find_by(key: 'XXXX')
  Metadata::Object Load (0.3ms)  SELECT `metadata_objects`.* FROM `metadata_objects` WHERE `metadata_objects`.`key` = 'XXXX' LIMIT 1

but in controllers, it can't resolve the model ::Metadata::Object.find_by(key: 'XXXX')

NameError Exception: uninitialized constant Metadata::Object

question from:https://stackoverflow.com/questions/65868186/rails-6-api-only-namespace-models-can-not-resolve-the-namespace-models-in-co

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...