Skip to content

Instantly share code, notes, and snippets.

@sld
Forked from clemens/product.rb
Created March 13, 2012 12:38
Show Gist options
  • Save sld/2028522 to your computer and use it in GitHub Desktop.
Save sld/2028522 to your computer and use it in GitHub Desktop.
the query selects all ids from the full branch from the nested set
class Product
define_index do
# ...
has %(
(
SELECT GROUP_CONCAT(IFNULL(parent.`id`, '0') SEPARATOR ',')
FROM `categories` AS node,
`categories` AS parent
WHERE node.`merchant_id` = `products`.`merchant_id` AND
parent.`merchant_id` = `products`.`merchant_id` AND
node.`lft` BETWEEN parent.`lft` AND parent.`rgt` AND node.`id` = `category_id`
ORDER BY parent.`lft`
)
), :source => :query, :type => :multi, :as => :category_ids, :facet => true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment