0

I have multiple models setup and I am trying to search through them all. However, some models have specific conditions that need to be satisfied. A basic example of my search:

@search = Sunspot.search [User, Event, Story, Album, Photo] do
  any_of do # Return anything that matches any of the scopes in this block
    all_of do # Return only those results that match these scopes
      with :class, Album # This limits scopes in this block to Album results
      with :private, false
    end
  end

  fulltext params[:q]
  with :deleted_at, nil
  order_by :created_at, :desc
end

In this example, I am trying to only show albums that are not private. This however doesn't work, nothing is returning. When I removed the any_of block, results return.

I referenced: Querying multiple models with different attributes using Sunspot

Similar issue: Searching across multiple models using sunspot/solr

Any help would be appreciated.

Community
  • 1
  • 1
user3023421
  • 333
  • 1
  • 7
  • 16

2 Answers2

0

So I managed to find a workaround

Instead of searching for the private condition, I am not indexing album records who are private

For those who stumble upon this, inside the Album model:

searchable :if => proc { |album| album.deleted_at == nil && album.private == false } do
  text :title
  text :description

  boolean :private
  time :created_at
end
user3023421
  • 333
  • 1
  • 7
  • 16
0

I think you are looking for something similar to this I guess, this is happening due to some weird scenario. I'm not sure what it is.

But I have found another way to solve this. I have added my answer here: Querying multiple models with different attributes using Sunspot

@search = Sunspot.search [User, Event, Story, Album, Photo] do
  any_of do # Return anything that matches any of the scopes in this block
    all_of do # Return only those results that match these scopes
      with :class, Album # This limits scopes in this block to Album results
      with :private, false
    end

    all_of do
      with :deleted_at, nil
    end
  end

  fulltext params[:q]
  order_by :created_at, :desc
end
Community
  • 1
  • 1