将 Rails 应用程序环境加载到 Jupyter 笔记本后模型常量未定义
Model constants undefined after loading Rails app environment into a Jupyter notebook
我来自 Python 背景,正在 Rails 学习 Ruby。
我想使用 Jupyter 笔记本来操作 Rails 对象作为学习练习。 (我正在关注Rails Getting Started Guide。)
阅读几篇关于如何为脚本加载 Rails 应用程序环境的博文后,我设法在 Jupyter 笔记本的单元格内创建了模型实例。
然而,为了这样做,我不得不调用 Rails.autoload "Article"
来创建文章的新实例。
这是具有 Ruby 2.3 内核的 Jupyter 笔记本中的代码 运行。 Rails版本是4.2.6
ENV['RAILS_ENV'] = 'development'
require ::File.expand_path('../config/boot', __FILE__)
require ::File.expand_path('../config/environment', __FILE__)
Rails.autoload "Article" # if I omit this line, Article is an undefined constant
article = Article.new(text: "test")
article.save
我似乎快要能够在我的 Jupyter notebook 中操作 Rails 相关对象了。
我的问题是:缺少什么以便我不必为 Article 或任何其他模型调用 Rails.autoload "Article"
?
提前致谢。
我找到了解决方案 here。
将密码更改为应用程序的目录似乎是缺少的步骤。
FileUtils.cd('/home/ubuntu/projects/ruby-docker-heroku/blog')
require './config/boot'
require './config/environment'
Article # no more undefined constant error
我来自 Python 背景,正在 Rails 学习 Ruby。
我想使用 Jupyter 笔记本来操作 Rails 对象作为学习练习。 (我正在关注Rails Getting Started Guide。)
阅读几篇关于如何为脚本加载 Rails 应用程序环境的博文后,我设法在 Jupyter 笔记本的单元格内创建了模型实例。
然而,为了这样做,我不得不调用 Rails.autoload "Article"
来创建文章的新实例。
这是具有 Ruby 2.3 内核的 Jupyter 笔记本中的代码 运行。 Rails版本是4.2.6
ENV['RAILS_ENV'] = 'development'
require ::File.expand_path('../config/boot', __FILE__)
require ::File.expand_path('../config/environment', __FILE__)
Rails.autoload "Article" # if I omit this line, Article is an undefined constant
article = Article.new(text: "test")
article.save
我似乎快要能够在我的 Jupyter notebook 中操作 Rails 相关对象了。
我的问题是:缺少什么以便我不必为 Article 或任何其他模型调用 Rails.autoload "Article"
?
提前致谢。
我找到了解决方案 here。
将密码更改为应用程序的目录似乎是缺少的步骤。
FileUtils.cd('/home/ubuntu/projects/ruby-docker-heroku/blog')
require './config/boot'
require './config/environment'
Article # no more undefined constant error