LearningRoR
LearningRoR

Reputation: 27232

How to search by association with Sunspot?

I have 2 models:

class UserPrice < ActiveRecord::Base
  attr_accessible :price, :product_name
  belongs_to :user
  belongs_to :product

  searchable do
     integer :product_id do
       product.map(&:name)
     end
   end

  # Associations on form like "choosing a category".
  def product_name
    product.name if product
  end

  def product_name=(name)
    self.product = Product.find_or_create_by_name(name) unless name.blank?
  end
end

---------------------------------------------------------------------------------
class Product < ActiveRecord::Base
  attr_accessible :name
  has_many :user_prices
  has_many :users, :through => :user_prices
end

I want to search inside of my UserPrice model but search by the Products name. How is that done inside of the block?

Upvotes: 1

Views: 1213

Answers (1)

drhenner
drhenner

Reputation: 2230

class UserPrice < ActiveRecord::Base
  belongs_to :product

  searchable do
    text :product_name do
      product.name
    end
  end
end

Upvotes: 4

Related Questions