<!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"> 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:<br>
<br>
class Something<br>
acts_as_nested_set<br>
<br>
def movie_children<br>
find_children(:movies)<br>
end<br>
<br>
def books_children<br>
find_children(:books)<br>
end<br>
<br>
# Or... if you really want to get fancy with meta programming, you
can just define this method and<br>
# then just call <i>s = Something.find(some_id_variable);
s.movie_children<br>
<br>
</i> def method_missing(method_name, *args, &block)<br>
matches = /^([^_]+)_children$/.match(method_name)<br>
matches ? send(:find_children, matches[1].pluralize) :
super(method_name, *args, &block)<br>
end<br>
<br>
private <br>
def find_children(col_name)<br>
</font></font><font size="-1"><font
face="Helvetica, Arial, sans-serif">self.class.find(:all, :conditions
=> ["lft BETWEEN ? AND ? AND `#{col_name}` = 1", self.lft, self.rgt])</font></font><br>
<font size="-1"><font face="Helvetica, Arial, sans-serif"> 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:!&!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 & 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
<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>