LoadError:期望{app_path}/models/model file.rb定义模型名称 [英] LoadError: Expected {app_path}/models/model file.rb to define model name

查看:106
本文介绍了LoadError:期望{app_path}/models/model file.rb定义模型名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行此程序时出现此错误

I am getting this error While running this

LoadError: Expected /home/user/Desktop/Tripurari/myapp/app/models/host.rb to define Host##

但是所有东西都摆在那儿.有人可以告诉我以下方法的确切问题是什么.

But every thing on it's place. Can some one tell me what the exact problem is below method.

def self.check_all(keyword)
  memo_mutex = Mutex.new
  memo       = {}
  threads    = []
  name       = keyword.keyword
  SITES.each do |site_and_options|
    threads << Thread.new do
      @host = Host.find_or_create_by_name(site)
      if keyword.unavailable_usernames.find_by_host_id(@host.id)
        memo[@host.name] = true
      else
        memo[@host.name] = false
      end
    end
  end
  threads.each { |t| t.join }
  memo
end

推荐答案

该问题可能是由自动加载器引起的.如果Host类在首次进入创建几个新线程的循环时尚未加载,则它将自动加载,即Rails在加载路径中搜索与命名约定匹配的文件并要求它.

The issue is probably caused by the autoloader. If the Host class is not yet loaded when first entering the loop where you create a couple of new threads, it is autoloaded, i.e. Rails searches the loadpath for a file matching the naming conventions and requires it.

此过程不是线程保存的.在您的情况下,当您快速连续创建服务线程时,每次尝试自动加载 global 类时,都会出现争用条件,并且会发生奇怪的事情.基本上,有两种解决方法:

This process is not threadsave. In your case, as you are creating servral threads in quick succession, each trying to autoload the global class, you get race conditions and strange things happen. Basically, you have two options for tackling this:

  1. 您可以在启动循环之前使用require 'host'显式加载模型,然后再启动线程.
  2. 或者您可以在初始化程序中设置config.threadsave!. (除其他事项外)这将在启动服务器时预加载所有类.这是首选方法,因为这样可以避免大量其他难以调试的并发问题.有关config.threadsafe!的更多信息,请参阅亚伦·帕特森(Aaron Patterson)的优秀文章应该在Rails 4中将其完全删除.
  1. You can explicitly load the model before starting your threads by using require 'host' before starting your loop.
  2. Or you can set config.threadsave! in an initializer. This will (among other things) preload all your classes when starting your server. This is preferred as with this, you avoid a truckload of other difficult to debug concurrency issues. For more information about config.threadsafe!, please refer to the excellent article by Aaron Patterson arguing it should be removed altogether in Rails 4.

这篇关于LoadError:期望{app_path}/models/model file.rb定义模型名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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