<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<font size="-1"><font face="Helvetica, Arial, sans-serif">&nbsp;&nbsp;&nbsp; I think I
have the trick for what you need.&nbsp; You want all types of things below a
certain category, right?&nbsp; Rather than using completely different
models, just add a few methods to the one model that keeps track of
everything like so:<br>
<br>
class Something<br>
&nbsp; acts_as_nested_set<br>
<br>
&nbsp; def movie_children<br>
&nbsp;&nbsp;&nbsp; find_children(:movies)<br>
&nbsp; end<br>
<br>
&nbsp; def books_children<br>
&nbsp;&nbsp;&nbsp; find_children(:books)<br>
&nbsp; end<br>
<br>
&nbsp; # Or... if you really want to get fancy with meta programming, you
can just define this method and<br>
&nbsp; # then just call <i>s = Something.find(some_id_variable);
s.movie_children<br>
<br>
</i>&nbsp; def method_missing(method_name, *args, &amp;block)<br>
&nbsp;&nbsp;&nbsp; matches = /^([^_]+)_children$/.match(method_name)<br>
&nbsp;&nbsp;&nbsp; matches ? send(:find_children, matches[1].pluralize) :
super(method_name, *args, &amp;block)<br>
&nbsp; end<br>
<br>
&nbsp; private <br>
&nbsp;&nbsp; def find_children(col_name)<br>
&nbsp;&nbsp;&nbsp;&nbsp; </font></font><font size="-1"><font
 face="Helvetica, Arial, sans-serif">self.class.find(:all, :conditions
=&gt; ["lft BETWEEN ? AND ? AND `#{col_name}` = 1", self.lft, self.rgt])</font></font><br>
<font size="-1"><font face="Helvetica, Arial, sans-serif">&nbsp;&nbsp; end<br>
end<br>
</font></font>
<div class="moz-signature">
<div style="line-height: 125%;"> <img
 src="cid:part1.02020504.00010106@gnexp.com" height="49" width="136"><br>
<span style="font-family: Arial,Helvetica,sans-serif; font-size: 9pt;"><b>Jeremy
Nicoll</b><br>
<a href="http://www.gnexp.com">www.gnexp.com</a><br>
(801) 783-3831 </span>
</div>
</div>
<br>
<br>
Larry E. Lutz wrote:
<blockquote
 cite="mid:!&amp;!AAAAAAAAAAAYAAAAAAAAAJEa5KrSnKZOu8NPJG9qB4fCgAAAEAAAAHVAQaWVB%2FxHq%2Fhux43R+lIBAAAAAA==@swbell.net"
 type="cite">
  <pre wrap="">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 &amp; Collectibles
     32       null                 Performing Arts
2507           32                Business Aspects
2511           32                Film &amp; 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 &amp; Collectibles
  32   Performing Arts
2507   Performing Arts &gt; Business Aspects
2511  Performing Arts &gt; Film &amp; Video
3566  Performing Arts &gt; Film &amp; Video &gt; Genres
3567  Performing Arts &gt; Film &amp; Video &gt; Genres &gt; Setting
3573  Performing Arts &gt; Film &amp; Video &gt; Genres &gt; Setting &gt; Crime
3574  Performing Arts &gt; Film &amp; Video &gt; Genres &gt; Setting &gt; Film Noir
3568  Performing Arts &gt; Film &amp; Video &gt; Genres &gt; Mood
2539 Performing Arts &gt; Film &amp; Video &gt; 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 
<a class="moz-txt-link-abbreviated" href="mailto:lutzle@swbell.net">lutzle@swbell.net</a> 


  </pre>
  <pre wrap="">
<hr size="4" width="90%">
_______________________________________________
Betternestedset-talk mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Betternestedset-talk@rubyforge.org">Betternestedset-talk@rubyforge.org</a>
<a class="moz-txt-link-freetext" href="http://rubyforge.org/mailman/listinfo/betternestedset-talk">http://rubyforge.org/mailman/listinfo/betternestedset-talk</a>
  </pre>
</blockquote>
</body>
</html>