简体   繁体   English

在带有 Phobos Gem 的 Rails 6 中找不到 ActiveRecord Class

[英]Cannot find ActiveRecord Class in Rails 6 with Phobos Gem

I am using the phobos gem to consume Kakfa Messages in a Rails App and I have it configured within the rails config/initializers/phobos.rb to start an executor from the config/phobos.yml like the the following:我正在使用phobos gem在 Rails 应用程序中使用 Kakfa 消息,并在 rails config/initializers/phobos.rb它以从config/phobos.yml启动executor程序,如下所示:

config/initializers/phobos.rb : config/initializers/phobos.rb

Phobos.configure('config/phobos.yml')
executor = Phobos::Executor.new
executor.start

I get a :exception_class=>"NameError", :exception_message=>"uninitialized constant TransactionsConsumer::Address" error when trying to load the Address model from ActiveRecord .尝试从ActiveRecord加载Address model 时出现:exception_class=>"NameError", :exception_message=>"uninitialized constant TransactionsConsumer::Address"错误。 There should be no TransactionsConsumer::Address but it should find the Address class in app/models/address.rb .应该没有TransactionsConsumer::Address但它应该在app/models/address.rb中找到Address class 。

My Consumer in app/consumers/transactions_consumer.rb :我的消费者在app/consumers/transactions_consumer.rb

class TransactionsConsumer
  include Phobos::Handler

  def self.start(kafka_client)
    # setup handler
    puts "Starting Transaction Consumer"
  end

  def consume(payload, metadata)
    puts metadata
    puts payload
    data = JSON.parse(payload)
    if Address.exists?(address: data["fromAddress"])
      AccountBalanceUpdateJob.perform_in(1, data["fromAddress"])
    end
  end
end

I assume I'm not configuring Phobos correctly in some way to autoload or see the Rails Classes.我假设我没有以某种方式正确配置 Phobos 以自动加载或查看 Rails 类。 How do I get Phobos configured to work with Rails models/objects?如何将 Phobos 配置为使用 Rails 模型/对象?

Does the TransactionConsumer class need to include or require something special? TransactionConsumer class 是否需要includerequire一些特殊的东西?

This issue was my own fault.这个问题是我自己的错。 I had renamed the Address class to Account and I thought it was because of the way I was starting rails and phobos我已将Address class 重命名为Account ,我认为这是因为我启动railsphobos的方式

Changing:改变:

if Address.exists?(address: data["fromAddress"])
  AccountBalanceUpdateJob.perform_in(1, data["fromAddress"])
end

To:至:

if Account.exists?(address: data["fromAddress"])
  AccountBalanceUpdateJob.perform_in(1, data["fromAddress"])
end

Fixed the issue.修复了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM