Question

I would like to add a conditionally index some data inside the sunspot 'searchable' method in my model. Ideally it would look something like this:

searchable do
    string :important_text
    if address_visible?
      string :address
    end
end

In the above example, I would like to index the address field only if the address_visible? method (on the model) returns true. Unfortunately, the address_visible? method throws a 'NoMethodError' because the context is now a Sunspot::DSL::Fields, not the model.

Was it helpful?

Solution

I don't think you can actually do exactly what you want. Nonetheless you could index a different value for address when address is not visible. For example:

searchable do
    string :important_text
    string :address { |model| model.address_visible? ? model.address : '' }
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top