Larry E. Lutz
2008-Mar-21 16:28 UTC
[Betternestedset-talk] Retrieving a Subset of the "Tree"
Actually, I failed to mention that I had tried the scope/conditions with both "true" and "1." Rails should make the appropriate substitution of "1" for "true" as it does in a migration and other places, but just to be sure, I tried it both ways. It is correct, theoretically, that movies could be scattered all over the database - or more correctly, the movie genres could be scattered all over the database. ("Categories" is merely a list of "subject headings," if you will, that will be connected to the table that contains the information on the movies themselves, through a join table. The same would happen for the "books" table, the "albums" table, etc.) That''s why I''ve used a Boolean field to designate which particular category rows are to be associated with movie genres, and that''s why I need to pull on that value. In actual practice, movie genres do happen to be clumped together in the database. Perhaps an example would make this clearer. The following is a sampling of the entries in my table (minus the lft and rgt columns): Id parent_id topic 1 null Antiques & Collectibles 32 null Performing Arts 2507 32 Business Aspects 2511 32 Film & Video 2539 2511 Screenwriting 3566 2511 Genres 3567 3566 Setting 3568 3566 Mood 3573 3567 Crime 3574 3567 Film Noir All of these entries are possible "subjects" for "books." Hence, the Boolean field "books" would be set to true (or 1, which is the equivalent for true in MySQL). However, only those with the id''s 3566, 3567, 3568, 3573, and 3574 are appropriate to "music genres" and would have their associate "movies" Boolean field set to true (or 1). My goal is to be able to display a selection list for a particular type of thing that has only the categories that are appropriate to that kind of thing so that (a) the user doesn''t have to wade through irrelevant categories and (b) the user doesn''t inadvertently select a category that is inappropriate to that particular kind of thing. Because any given category, as in this example, can apply to multiple kinds of things, it just makes sense to have all the categories in the same table. By the way, in my system, this sample listing would display as 1 Antiques & Collectibles 32 Performing Arts 2507 Performing Arts > Business Aspects 2511 Performing Arts > Film & Video 3566 Performing Arts > Film & Video > Genres 3567 Performing Arts > Film & Video > Genres > Setting 3573 Performing Arts > Film & Video > Genres > Setting > Crime 3574 Performing Arts > Film & Video > Genres > Setting > Film Noir 3568 Performing Arts > Film & Video > Genres > Mood 2539 Performing Arts > Film & Video > Screenwriting What is truly scary to me is that this kind of categorization of things within a consistent, hierarchical framework is one of the most common aspects of human life. It''s done everyday in everything from libraries to botany to genealogy, yet it seems overwhelmingly difficult to do on a computer. Larry E. Lutz lutzle at swbell.net -------------- next part -------------- A non-text attachment was scrubbed... Name: winmail.dat Type: application/ms-tnef Size: 11522 bytes Desc: not available Url : http://rubyforge.org/pipermail/betternestedset-talk/attachments/20080321/30875028/attachment.bin
Jeremy Nicoll
2008-Mar-21 18:10 UTC
[Betternestedset-talk] Retrieving a Subset of the "Tree"
I think I have the trick for what you need. You want all types of things below a certain category, right? Rather than using completely different models, just add a few methods to the one model that keeps track of everything like so: class Something acts_as_nested_set def movie_children find_children(:movies) end def books_children find_children(:books) end # Or... if you really want to get fancy with meta programming, you can just define this method and # then just call /s = Something.find(some_id_variable); s.movie_children / def method_missing(method_name, *args, &block) matches = /^([^_]+)_children$/.match(method_name) matches ? send(:find_children, matches[1].pluralize) : super(method_name, *args, &block) end private def find_children(col_name) self.class.find(:all, :conditions => ["lft BETWEEN ? AND ? AND `#{col_name}` = 1", self.lft, self.rgt]) end end *Jeremy Nicoll* www.gnexp.com <http://www.gnexp.com> (801) 783-3831 Larry E. Lutz wrote:> Actually, I failed to mention that I had tried the scope/conditions with > both "true" and "1." Rails should make the appropriate substitution of "1" > for "true" as it does in a migration and other places, but just to be sure, > I tried it both ways. > > It is correct, theoretically, that movies could be scattered all over the > database - or more correctly, the movie genres could be scattered all over > the database. ("Categories" is merely a list of "subject headings," if you > will, that will be connected to the table that contains the information on > the movies themselves, through a join table. The same would happen for the > "books" table, the "albums" table, etc.) That''s why I''ve used a Boolean > field to designate which particular category rows are to be associated with > movie genres, and that''s why I need to pull on that value. > > In actual practice, movie genres do happen to be clumped together in the > database. Perhaps an example would make this clearer. The following is a > sampling of the entries in my table (minus the lft and rgt columns): > > Id parent_id topic > 1 null Antiques & Collectibles > 32 null Performing Arts > 2507 32 Business Aspects > 2511 32 Film & Video > 2539 2511 Screenwriting > 3566 2511 Genres > 3567 3566 Setting > 3568 3566 Mood > 3573 3567 Crime > 3574 3567 Film Noir > > All of these entries are possible "subjects" for "books." Hence, the Boolean > field "books" would be set to true (or 1, which is the equivalent for true > in MySQL). However, only those with the id''s 3566, 3567, 3568, 3573, and > 3574 are appropriate to "music genres" and would have their associate > "movies" Boolean field set to true (or 1). > > My goal is to be able to display a selection list for a particular type of > thing that has only the categories that are appropriate to that kind of > thing so that (a) the user doesn''t have to wade through irrelevant > categories and (b) the user doesn''t inadvertently select a category that is > inappropriate to that particular kind of thing. Because any given category, > as in this example, can apply to multiple kinds of things, it just makes > sense to have all the categories in the same table. > > By the way, in my system, this sample listing would display as > > 1 Antiques & Collectibles > 32 Performing Arts > 2507 Performing Arts > Business Aspects > 2511 Performing Arts > Film & Video > 3566 Performing Arts > Film & Video > Genres > 3567 Performing Arts > Film & Video > Genres > Setting > 3573 Performing Arts > Film & Video > Genres > Setting > Crime > 3574 Performing Arts > Film & Video > Genres > Setting > Film Noir > 3568 Performing Arts > Film & Video > Genres > Mood > 2539 Performing Arts > Film & Video > Screenwriting > > What is truly scary to me is that this kind of categorization of things > within a consistent, hierarchical framework is one of the most common > aspects of human life. It''s done everyday in everything from libraries to > botany to genealogy, yet it seems overwhelmingly difficult to do on a > computer. > > Larry E. Lutz > lutzle at swbell.net > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Betternestedset-talk mailing list > Betternestedset-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/betternestedset-talk >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/betternestedset-talk/attachments/20080321/6bb78d80/attachment-0001.html -------------- next part -------------- A non-text attachment was scrubbed... Name: logo_sig.jpg Type: image/jpeg Size: 2312 bytes Desc: not available Url : http://rubyforge.org/pipermail/betternestedset-talk/attachments/20080321/6bb78d80/attachment-0001.jpg