equivalentideas
equivalentideas

Reputation: 325

Thinking Sphinx, search ordered by associate's attribute

I'm trying to order the Thinking Sphinx search results for a model Item (real-time index) by an attribute on an associated object, Document:

 @items = Item.search(
  'jitters',
  sql: {
    left_joins: :document,
    order: 'documents.published_at DESC, items.created_at DESC'
  }
)

This works correctly when there are results.

However, when Thinking Sphinx finds no results, it returns <NoMethodError: undefined method 'any?' for nil:NilClass>.

I would expect it to return the usual 'Nothing found result.

Does anyone know the correct way to make this query? Or have I found a bug?

Upvotes: 0

Views: 60

Answers (1)

pat
pat

Reputation: 16226

The error may be due to the left_joins option - I have a feeling that's not supported in Thinking Sphinx.

However, the better approach here would be to include the Document's published_at time in your Item index, so Sphinx itself can do the sorting:

# within the index definition:
has published_at
has document.published_at, :as => :document_published_at

# and then when searching:
Item.search "jitters", :order => "document_published_at DESC, published_at DESC"

Upvotes: 1

Related Questions