Rails 模型不采用配置值

Rails Model not taking configuration value

我创建了一个自定义配置参数,但我的用户模型无法访问它的值。

configuration.rb中:

config.x.user.starting_cash = 50

在我的用户模型中:

  # Create wallet for each new user
  after_create :create_wallet

  # Block for creating a new wallet for each new user
  def create_wallet
    @wallet = Wallet.new(user_id: self.id, cash: Rails.configuration.x.user.starting_cash)
    @wallet.save
  end

它确实为新用户创建了一个钱包,但是 cash 的值是空的,而不是 50。当手动将现金设置为 50 的整数时,它工作正常:

@wallet = Wallet.new(user_id: self.id, cash: 50)

当我在 Rails 控制台中访问配置参数时,它按预期工作:

2.6.3 :022 > Rails.configuration.x.user.starting_cash
 => 50 

我在其中搜索解决方案的文档包括:https://guides.rubyonrails.org/configuring.html#custom-configuration

解决了。我必须重新启动(开发)服务器才能加载配置参数。不知道。

我猜这是初学者的错误,但我确实觉得 Rails 文档对此可能更明确一些,因为我们不必重新启动服务器来更改例如控制器。