重新加载 rails 控制台 [英] Reload the rails console

查看:42
本文介绍了重新加载 rails 控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于 Rails 控制台的使用,当我对模型进行一些更改时,是否每次都需要重新加载 Rails 控制台以反映该更改?

Regarding the use of Rails console, when I make some change on a model, do I need to reload the rails console every time to make that change reflects?

例如,我的原始代码如下:

For example, I have my original code as follows:

class Article < ActiveRecord::Base
  validates :title, :presence => true
  validates :body, :presence => true
end

稍后,我想添加一些额外的属性,如下所示.

Later, I want to add some additional attribute as below.

class Article < ActiveRecord::Base
  validates :title, :presence => true
  validates :body, :presence => true

  def long_title
    "#{title} - #{published_at}"
  end
end

是否需要运行命令reload!"每次都能做long_title"方法调用?否则,我会收到一条错误消息,指出该属性尚未定义.为什么我们需要手动执行?

Does it need to run the command "reload!" every time to be able to do the "long_title" method call? Otherwise, I will get an error saying as the attribute hasn't been defined. and Why do we need to perform that manually?

推荐答案

是的,您需要调用 reload! 因为这将清除已加载的已加载常量,并将按原样加载它们'在控制台中重新引用.

Yes, you need to call reload! as this will clear the loaded constants that have been loaded and will load them as they're referenced in the console.

如果您有 reload! 之前的旧对象,您将需要对这些单个对象调用 reload 或查找新对象并使用它们(如果您想尝试)出新方法.

If you have old objects from before the reload! you will need to call reload on these individual objects or find new objects and work with them if you want to try out the new method.

作为替代方案,我真的建议您研究一个测试框架,例如 RSpec,它为您的应用程序提供可重复的测试和安全网.

As an alternative, I would really recommend looking into a testing framework such as RSpec which gives you repeatable tests and a safety net for your application.

看起来您正在尝试将控制台用作应用程序中新功能的测试工具,而 RSpec 更适合这种情况.控制台非常适合实验.

It looks like you're trying to use the console as a testing tool for new functionality in your application, which is what RSpec is better suited for. The console is really good for experimentation.

这篇关于重新加载 rails 控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆