Ruby-on-rails Rails 控制台:重新加载!不反映模型文件的变化?可能的原因是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5427596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails Console: reload! not reflecting changes in model files? What could be possible reason?
提问by Maddy.Shik
Earlier it was working fine. I have been playing little bit config. So may be i have changed some config unknowingly.
早些时候它工作正常。我一直在玩一点点配置。所以可能是我在不知不觉中改变了一些配置。
here is config of environment/development.rb
这是 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'
Here is config section of application.rb
这是 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
when i run reload! on rails console it return true
当我运行重新加载!在 Rails 控制台上它返回 true
回答by Nazar Hussain
reload!only reloads the latest code in the console environment. It does not re-initialize existing objects.
reload!只在控制台环境中重新加载最新的代码。它不会重新初始化现有对象。
This means if you have already instantiated any objects, their attributes would not be updated - including newly introduced validations. However, if you create a new object, its attributes (and also validations) will reflect the reloaded code. more here
这意味着如果您已经实例化了任何对象,它们的属性将不会更新——包括新引入的验证。但是,如果您创建一个新对象,其属性(以及验证)将反映重新加载的代码。 更多在这里
回答by ctide
Are you reloading the object from the database?
您是否从数据库重新加载对象?
For example:
例如:
>> a = User.last
=> #<User id: 16, email: "[email protected]">
>> reload!
Reloading...
=> true
'a' won't reflect any changes to your model until you reload it from the db.
在您从数据库重新加载模型之前,'a' 不会反映对模型的任何更改。

