NoMethodError undefined `fields' method for nil: NilClass

Usage: Rails 3.1.1

I am trying to create a search engine in my application that scans a large database (about 100,000 items) for string matches.

I am using the following:

fp = FeededProduct.where("name LIKE '%blue%' OR description LIKE '%blue%' OR category LIKE '%blue%'") 

for the search query for "blue".

When I run the equivalent search phrase in MySQL, it works fine, but when I try to run it in rails, it shows:

 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%') 

Troubleshooting and troubleshooting:

This happens only for large search results, I could not distinguish the number, but it failed when it should return 920 results, but it did NOT crash when returning 6 results!

My conclusion from the foregoing is that it cannot store all the results of 920 in memory OR that there is some type of row that causes it to fall, and the more results, the more likely it will contain this type row. I am more inclined to the first conclusion.

I cannot fix it very well because when I try to fail (with the same error code):

 raise fp.inspect 

It also crashes for:

 fp.each do |prod| begin puts 'Do nothing' rescue puts 'Something crashed' end 

but it does NOT crash for:

 raise fp.count.inspect 

So I have a memory problem? What can I do to solve the problem further and / or solve the problem?

Use: Mac OS X 10.7.2. a lion
Database: InnoDB
Adapter: Mysql2 (don't know which version)

Stack

 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' 

Change 2012-03-06 Additional problem:

  • I tried using

    fp2 = FeededProduct.limit (60000)

to create a really big array of hits and it worked great. Therefore, I suppose that excludes my assumption that the fp variable cannot contain a certain number of elements.

The core of the problem seems to be if I use:

 fp = FeededProduct.where("name LIKE '%blue%' OR description LIKE '%blue%' OR category LIKE '%blue%'") 

I cannot use the fp variable for anything afterwards without crashing the application.

+4
source share
4 answers

I replaced mysql instead of the mysql2 adapter and solved the problem. Thanks everyone for trying! I learned a lot to remove your suggestions.

+1
source

Can you post the code on line 190 to search_results_controller.rb and any others that may reference the "fields" attribute in your show method? Also relevant parts of your FeededProduct model from / models / feeded _product.rb

 app/controllers/search_results_controller.rb:190:in `show' 

It is not clear which fields relate to the information that you published. This could be a typo, bad code, or a migration that must be running.

Also note that

 fp.each.do |prod| 

is not syntactically correct. It should be:

 fp.each do |prod| 
+1
source

What if you moved this fp logic to your model?

 class Item < ActiveRecord::Base ... def self.feeded_products(query) self.where("name LIKE '%#{query}%' OR description LIKE '%#{query}%' OR category LIKE '%#{query}%'") end ... end 

... and then in your controller you can simply call:

 Item.feeded_products(query) 

Hope this helps.

+1
source

In such situations, when you have large objects, you can use the find_each and find_in_batches methods or some kind of pagination.

0
source

Source: https://habr.com/ru/post/1399035/


All Articles