I have three tables, categories
, tags
, and taggings
. The categories table is put together in the fashion of a nested set, with relevant columns id
, lft
, rgt
, and parent_id
. Tags has id
and name
. Taggings has tag_id
and taggable_id
, which refers to categories.id
.
If possible, I'd like for one query which returns in a field, say tag_list
, a string containing a category's and all of its ancestors' tags. So given the following schema:
Categories
id | parent_id | lft | rgt
1 | NULL | 1 | 6
2 | 1 | 2 | 5
3 | 2 | 3 | 4
Tags
id | name
1 | cool
2 | rad
3 | soup
Taggings
id | tag_id | taggable_id
1 | 1 | 1
2 | 2 | 2
3 | 3 | 3
I'd like for the query SELECT ??? FROM categories
to return:
id | tag_list
1 | cool
2 | rad cool
3 | rad cool soup
Background info: I'm running Rails, and I'm using Thinking Sphinx for search and awesome_nested_set for nesting. I have a table called categories
, which has many tags
in a has_many_through
relationship.