【问题标题】:Rails Console: reload! not reflecting changes in model files? What could be possible reason?Rails 控制台:重新加载!不反映模型文件的变化?可能的原因是什么?
【发布时间】:2011-07-22 14:32:27
【问题描述】:

之前它运行良好。我一直在玩一点点配置。所以可能是我在不知不觉中更改了一些配置。

这里是 environment/development.rb 的配置

  config.cache_classes = false

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_view.debug_rjs             = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log

  # Only use best-standards-support built into browsers
  config.action_dispatch.best_standards_support = :builtin

  # migration prefix with sequence #s
  config.active_record.timestamped_migrations = false

  #time zone
  config.time_zone = 'UTC'

这是 application.rb 的配置部分

 # Configure the default encoding used in templates for Ruby 1.9.
 config.encoding = "utf-8"

 # Configure sensitive parameters which will be filtered from the log file.
 config.filter_parameters += [:password]

 config.active_record.schema_format = :sql

当我运行重新加载!在 Rails 控制台上它返回 true

【问题讨论】:

    标签: ruby-on-rails ruby config reload rails-console


    【解决方案1】:

    您是否正在从数据库中重新加载对象?

    例如:

    >> a = User.last
    => #<User id: 16, email: "asdfadsf@sdfdsf.com">
    >> reload!
    Reloading...
    => true
    

    “a”在您从数据库重新加载之前不会反映您的模型的任何更改。

    【讨论】:

    • 注意 - 即使在访问对象上的方法时也是如此。例如,如果您更改了类方法 foo() 的定义,那么在控制台中 a.foo 将不会使用新定义,除非您先重新加载 a。
    【解决方案2】:

    reload! 只在控制台环境中重新加载最新的代码。它不会重新初始化现有对象。

    这意味着如果您已经实例化了任何对象,它们的属性将不会被更新 - 包括新引入的验证。但是,如果您创建一个新对象,它的属性(以及验证)将反映重新加载的代码。 more here

    【讨论】:

    • 自定义验证怎么样?我已经定义了一些方法并注册了 validate。当我更改验证逻辑时,它不会反映在重新加载!
    • 它会在你重新初始化对象时反映出来。
    猜你喜欢
    • 2012-06-29
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 2020-05-03
    • 2012-08-04
    • 2011-09-15
    • 2013-10-18
    相关资源
    最近更新 更多