[Betternestedset-talk] Retrieving a Subset of the "Tree"
Larry E. Lutz
lutzle at swbell.net
Fri Mar 21 09:27:53 EDT 2008
I have a "categories" table, set up as a nested set, that I want to use to
categorize a number of different types of things, such as books, software,
movies, etc. Since the table is rather large (over 3,000 rows), I would like
to be able to pull subsets of the table for the different types of things. I
can't use a singleton because the same category can apply to more than one
type of thing, and in many cases, a category set for a type of thing may
start a level below the root. In lieu of that, I have several Boolean fields
within the table for the different types of things. The problem is that, no
matter where I try to apply a scope or condition, the system seems to ignore
it and, instead, returns the full dataset.
My schema looks like this:
create_table "categories", :force => true do |t|
t.column "parent_id", :integer
t.column "lft", :integer
t.column "rgt", :integer
t.column "topic", :string, :default => "", :null => false
t.column "description", :text
t.column "books", :boolean, :default => true
t.column "entities", :boolean, :default => false
t.column "movies", :boolean, :default => false
t.column "music", :boolean, :default => false
t.column "software", :boolean, :default => false
t.column "tech_articles", :boolean, :default => false
end
Within the data in this table, "Movie Genres," for instance, is not a root
level record. Instead, it is buried several layers deep under the
"Performing Arts" root. The identifier for Movie Genres is the Boolean field
"movies."
With set_table_name "categories", here are some of the things that I've
tried that don't work:
------------------------------------------
has_many :children, :class_name => "MovieGenre", :foreign_key =>
:parent_id, :conditions => "movies = true"
belongs_to :parent, :class_name => "MovieGenre"
----------------------------------------
has_many :children, :class_name => "MovieGenre", :foreign_key =>
:parent_id, :conditions => "movies = true"
belongs_to :parent, :class_name => "MovieGenre", :foreign_key =>
:parent_id, :conditions => "movies = true"
acts_as_nested_set :scope => 'movies = true'
has_many :children, :class_name => "MovieGenre"
belongs_to :parent, :class_name => "MovieGenre"
----------------------------------------------
Is there anyway to achieve what I need to accomplish? I really don't want to
go to multiple "categories" tables for each kind of thing because I need to
keep the categories consistent. By the way, I would also like to have a
"long name" returned when I pull the subset, like this:
def long_name
self_and_ancestors.map{ |a| a.topic}.join(' > ')
end
However, that's not absolutely essential. I do need to be able to use the
returned dataset both in a listing and in a select form field.
Any ideas folks?
Larry E. Lutz
2425 Sage Road, Apt. 53
Houston, Texas 77056
(713) 850-1358
lutzle at swbell.net
-------------- next part --------------
A non-text attachment was scrubbed...
Name: winmail.dat
Type: application/ms-tnef
Size: 11734 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/betternestedset-talk/attachments/20080321/9e801f9c/attachment-0001.bin
More information about the Betternestedset-talk
mailing list