0

With this model:

class Product < ActiveRecord::Base
    belongs_to :category

    define_index do
        join category

        indexes sku
        indexes category.name, :as => :category_name, :facet => true
    end
end

When I do:

products = Product.search("something")
products[0].category_name if products.size > 0

I get error:

NoMethodError: undefined method `category_name'

How can I fix it?

Sebtm
  • 7,002
  • 8
  • 29
  • 32

2 Answers2

0

Thinking Sphinx does not create a new method for you - so you'll need to do it yourself, or just use the standard Rails approach to get the name:

products = Product.search 'something'
products.first.category.name unless products.empty?

# or add the following to your Product model:
def category_name
  category.name
end

# and then....
products = Product.search 'something'
products.first.category_name unless products.empty?
pat
  • 16,116
  • 5
  • 40
  • 46
  • I solved in this way: Product.search("something", :select=>"products.*, category.name AS category_name", :joins=>[:category]) – Sebtm Apr 24 '12 at 07:57
0

I solved in this way:

Product.search("something", :select=>"products.*, category.name AS category_name", :joins=>[:category])
Sebtm
  • 7,002
  • 8
  • 29
  • 32