Larry E. Lutz
2008-Mar-21 13:27 UTC
[Betternestedset-talk] Retrieving a Subset of the "Tree"
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
Jeremy Nicoll
2008-Mar-21 14:38 UTC
[Betternestedset-talk] Retrieving a Subset of the "Tree"
Ah, this one I do know about. :) If you are using MySQL, it actually needs to be: :conditions => "movies = 1" As there are no true boolean fields in MySQL - it''s actually just a tinyint with values of either 0 (for false) or 1 (for true). The only problem that I can possibly see with the above method is that it looks like (according to my good friend, Google) other databases /might/ store true/false queries differently and so if you were to need to switch to a different database then you /might/ have to change this code. I''m sure others could enlighten us on this subject. *Jeremy Nicoll* www.gnexp.com <http://www.gnexp.com> (801) 783-3831 Larry E. Lutz wrote:> 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 > > > > ------------------------------------------------------------------------ > > _______________________________________________ > 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/adcda7f7/attachment.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/adcda7f7/attachment.jpg
Jeremy Nicoll
2008-Mar-21 15:09 UTC
[Betternestedset-talk] Retrieving a Subset of the "Tree"
Of course, /children/ and /parent/ are also methods used by BNS, the
methods override ActiveRecord''s way of handling the associations.
Sorry
- did not quite get what you were asking before. It seems the children
method does not work as other AR associations either. So you can''t do
an AR find like /record.children.find(:all, :conditions =>
''something =
1'').
/To be quite honest, I''m still not sure what your intent is. Are
all movies going to be grouped together? It seems like with your db
setup that it is quite possible to have movies spread all over the
database, which when the records are by themselves would render the
/parent/ and /children/ methods quite useless. If you could explain
more fully why you want to do this and what the expected outcome is,
that would be most helpful.
*Jeremy Nicoll*
www.gnexp.com <http://www.gnexp.com>
(801) 783-3831
Jeremy Nicoll wrote:> Ah, this one I do know about. :) If you are using MySQL, it actually
> needs to be:
> :conditions => "movies = 1"
>
> As there are no true boolean fields in MySQL - it''s actually just
a
> tinyint with values of either 0 (for false) or 1 (for true). The
> only problem that I can possibly see with the above method is that it
> looks like (according to my good friend, Google) other databases
> /might/ store true/false queries differently and so if you were to
> need to switch to a different database then you /might/ have to change
> this code. I''m sure others could enlighten us on this subject.
>
>
> *Jeremy Nicoll*
> www.gnexp.com <http://www.gnexp.com>
> (801) 783-3831
>
>
> Larry E. Lutz wrote:
>> 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
>>
>>
>>
>>
------------------------------------------------------------------------
>>
>> _______________________________________________
>> Betternestedset-talk mailing list
>> Betternestedset-talk at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/betternestedset-talk
>>
> ------------------------------------------------------------------------
>
> _______________________________________________
> 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/cd525eb6/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/cd525eb6/attachment-0001.jpg
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/jpeg
Size: 2312 bytes
Desc: not available
Url :
http://rubyforge.org/pipermail/betternestedset-talk/attachments/20080321/cd525eb6/attachment-0001.jpe