NoMethodError 未定义的方法“字段”为 nil:NilClass

Chr*_*fer 5 memory ruby-on-rails nomethoderror

使用:Rails 3.1.1

我正在尝试在我的应用程序中创建一个搜索引擎,用于浏览大型数据库(大约 100 000 个项目)以进行字符串匹配。

我正在使用以下内容:

fp = FeededProduct.where("name LIKE '%blue%'  OR description LIKE '%blue%'  OR category LIKE '%blue%'")
Run Code Online (Sandbox Code Playgroud)

对于“blue”的搜索查询。

当我在 MySQL 中运行等效的搜索短语时,它工作正常,但是当我尝试在 rails 中运行它时,它显示:

NoMethodError: undefined method `fields' for nil:NilClass: SELECT `feeded_products`.* FROM `feeded_products`  WHERE (name LIKE '%blue%'  OR description LIKE '%blue%'  OR category LIKE '%blue%')
Run Code Online (Sandbox Code Playgroud)

线索和故障排除:

这只发生在大型搜索结果中,我无法区分一个数字,但它在应该返回 920 个结果时崩溃,但在返回 6 个结果时它不会崩溃!

我对上述结论的结论要么是它无法将所有 920 个结果保留在内存中,要么是某种类型的行使其崩溃,结果越多,它就越有可能包含该类型的行。我更倾向于第一个结论。

我无法很好地对其进行故障排除,因为它在我尝试运行时崩溃(具有相同的错误代码):

raise fp.inspect
Run Code Online (Sandbox Code Playgroud)

它也会崩溃:

fp.each do |prod| 
begin 
puts 'Do nothing'
rescue
puts 'Something crashed'
end
Run Code Online (Sandbox Code Playgroud)

但它不会崩溃:

raise fp.count.inspect 
Run Code Online (Sandbox Code Playgroud)

那么,我有记忆类型的问题吗?我该怎么做才能进一步解决这个问题和/或解决问题?

使用:Mac OS X 10.7.2。Lion
数据库:InnoDB
Adapter:Mysql2(不知道是哪个版本)

堆:

ActiveRecord::StatementInvalid (NoMethodError: undefined method fields' for nil:NilClass: SELECT feeded_products`.* FROM feeded_products WHERE (name LIKE '%blue%' OR description LIKE '%blue%' OR category LIKE '%blue%')): app/controllers/search_results_controller.rb:190:in `show'
Run Code Online (Sandbox Code Playgroud)

编辑 2012-03-06 附加故障排除:

  • 我试过

    fp2 = FeededProduct.limit(60000)

创建一个非常大的点击数组,并且效果很好。所以我想这排除了我的猜测,即 fp 变量不能容纳一定数量的项目。

问题的核心似乎是,如果我使用:

fp = FeededProduct.where("name LIKE '%blue%'  OR description LIKE '%blue%'  OR category LIKE '%blue%'")
Run Code Online (Sandbox Code Playgroud)

在应用程序不崩溃的情况下,我之后无法将 fp 变量用于任何事情。

Chr*_*fer 2

我更改为 mysql 而不是 mysql2 适配器,它解决了问题。感谢大家的尝试!我在解决你的建议时遇到了很多麻烦。