[Ruby] Filtering with multiple columns from related tables using sunspot

Tadashi Shigeoka ·  Tue, March 26, 2013

I created a slightly complex filtering condition using sunspot, a Ruby wrapper library for Solr, so here’s a memo.

# Sunspot/Search
searchable do
  # Filter by display setting ON (visibility.visible == true) AND
  # specified permission (role_id)
  integer :role_ids_with_visible, multiple: true do
    visibilities.select { |visibility| visibility.visible == true }
      .map { |visibility| visibility.role_id }
  end
end

In SQL, this is similar to joining related tables and filtering with something like “where visible = true and role_id = xxx”.

That’s all from the Gemba.