rehcelf
rehcelf

Reputation: 45

undefined method error rails 3 sunspot search

I can not figure out how to fix this issue!!! Please help!!

"undefined method `description' for nil:NilClass" 

results with the following code:

<ul>Test Search:
    <% @optsolr.each do |c| %>
      <li>
    <%= c.description %>
      </li>
<% end %>
</ul>

When I only call "c" instead of c.description the result is: Test Search:

   " [#<Nmodel id: 44, name: nil, price: nil, expiration: "2012-02-22", category: "Life ",      description: "my description text", user_id: 3, created_at: "2012-02-22 04:20:48", updated_at: "2012-02-22 04:20:48", quantity: nil>]"

My controller where i loop through a set of search parameters and search for a specific category:

              @search = Nmodel.search do
                  fulltext o.category do
                    query_phrase_slop 16
                 end
              end
              @optsolr[count[ = @search.results

Results from rails debug: --- - !!null - !!null - - !ruby/object:Nmodel attributes: id: 44 name: !!null price: !!null expiration: 2012-02-22 category: ! 'Life ' description: my description text user_id: 3 created_at: 2012-02-22 04:20:48.048506000Z updated_at: 2012-02-22 04:20:48.048506000Z quantity: !!null

Upvotes: 0

Views: 786

Answers (2)

Jak
Jak

Reputation: 3243

I guess from this Results from rails debug: --- - <<<<!!null >>>>>- <<<<!!null>>>>> - - <<<<!ruby/object:Nmodel attributes: id: 44.. it returning two empty record and one valid record.

Try this.

   <ul>Test Search:       
        <% @optsolr.delete_if{|ele| ele.blank?}.each do |c| %>
          <li>
        <%= c.description %>
          </li>
    <% end %>
    </ul>

Upvotes: 3

Rob Di Marco
Rob Di Marco

Reputation: 44992

One possibility...the sunspot search is returning an ID for an object that no longer exists in the database. This can happen, especially in development, if you have indexed an object and then deleted the row from the database (maybe by dropping the db?) without deleting it from Solr.

Upvotes: 0

Related Questions