سؤال

class Cat < ActiveRecord::Base
  searchable do
    string :color
  end
end
class Dog < ActiveRecord::Base
  searchable do
    string :color
  end
end

Given the models Cat and Dog above, how do I do a sunspot search for All dogs + only cats that are brown.

|dogs|color|    |cats|color|
------------    ------------
| 1  |black|    | 1  |black|
| 2  |brown|    | 2  |brown|
| 3  | red |    | 3  | red |

With the above dogs/cats, I want Dogs [1, 2 3] and Cat [2].

I want something like below, but condition the with to only apply to Cats.

Sunspot.search(Dog, Cat) do
  with :color, "brown"
end

Is there a with parameter that is the model type? If so I could do something like this:

Sunspot.search(Dog, Cat) do
  any_of do
    with :color, "brown"
    with :model, Dog
  end
end

How do you scope the Sunspot DSL pieces around a model being searched for?

هل كانت مفيدة؟

المحلول

I ended up adding a field table_name to the model index and used that.

class Dog < ActiveRecord::Base
  searchable do
    string :color
    string :table_name do Dog.table_name end
  end
end

Sunspot.search(Dog, Cat) do
  any_of do
    with :color, "brown"
    with :table_name, Dog.table_name
  end
end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top