I have categories and sub-categories in a table called “categories”.
Every row has a “parent” field to note if the entry is the sub-category
of another (never goes deeper than 1 level).
So there might be something like this:
id | name | parent
1 Dog 0
2 Pug 1
3 Siamese 5
4 Shih Tzu 1
5 Cat 0
6 Wiener Dog 1
7 Persian 5
I’d like to list it like
Dog
–Pug
–Shih Tzu
–Wiener Dog
Cat
–Siamese
–Persian
Right now it’s only listing them in one long list. But I’d like to list
each parent category and have the subcats listed under them.
Any ideas?
Josh P. wrote:
4 Shih Tzu 1
Cat
–Siamese
–Persian
Right now it’s only listing them in one long list. But I’d like to list
each parent category and have the subcats listed under them.
Any ideas?
If you can I would recommend redoing your db and models so that you have
categories and sub_categories. That would make this really easy. All
you would need to do is setup a category has_many :sub_categories and
rails would basically do the rest for you.
For instance if you setup the association then you could put this in a
view (set @categories = Category.find(:all) in your controller)
<%for category in @categories%>
<%for sub in category.sub_categories%>
<%=sub.name%>
<%end%>
<%end%>
Check out
http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html
for more info on associations.
Matthew M.
blog.mattmargolis.net
On Jun 16, 2006, at 10:01 PM, Josh P. wrote:
3 Siamese 5
Cat
–Siamese
–Persian
Right now it’s only listing them in one long list. But I’d like to
list
each parent category and have the subcats listed under them.
Recursion is the best way I know of to handle these things.
Here’s something that shows a tree correctly in text, I’ll
leave it to you to convert the output to HTML, I think I’d
choose nested lists.
This code is called via:
ShowOrganizations.as_tree
class ShowOrganizations
def self.as_tree
show Organization.find_by_name(‘Root’),0
end
def self.show(organization,level)
indent = ' ' * level
puts indent + 'org:' + organization.name
organization.children.each { |o| show o,level + 1 }
end
end
Additionally, if you want something pre-manufactured with
AJAX goodness, check out the LiveTree system.
http://www.epiphyte.ca/code/live_tree.html
–
– Tom M.
Matthew M. wrote:
Josh P. wrote:
4 Shih Tzu 1
Cat
–Siamese
–Persian
Right now it’s only listing them in one long list. But I’d like to list
each parent category and have the subcats listed under them.
Any ideas?
If you can I would recommend redoing your db and models so that you have
categories and sub_categories. That would make this really easy. All
you would need to do is setup a category has_many :sub_categories and
rails would basically do the rest for you.
For instance if you setup the association then you could put this in a
view (set @categories = Category.find(:all) in your controller)
<%for category in @categories%>
<%for sub in category.sub_categories%>
<%=sub.name%>
<%end%>
<%end%>
Nice. That worked…sort of. Do new controller/helpers/views need to be
created? I can’t figure out how I might could keep using the same ones.
Josh P. wrote:
I have categories and sub-categories in a table called “categories”.
Every row has a “parent” field to note if the entry is the sub-category
of another (never goes deeper than 1 level).
So there might be something like this:
id | name | parent
1 Dog 0
2 Pug 1
3 Siamese 5
4 Shih Tzu 1
5 Cat 0
6 Wiener Dog 1
7 Persian 5
I’d like to list it like
Dog
–Pug
–Shih Tzu
–Wiener Dog
Cat
–Siamese
–Persian
Right now it’s only listing them in one long list. But I’d like to list
each parent category and have the subcats listed under them.
Any ideas?
I am having exact same problem – I have been searching, asking, begging
for weeks…but have yet to see a good way to handle the selection of a
category.
http://tinyurl.com/28krhc #=> how do I do this with rails? (eBay
category selection).
I’m building a store and when I edit a product, i want to easily select
its category…which could be a sub-category, etc.etc.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Sat, Jun 17, 2006 at 07:01:16AM +0200, Josh P. wrote:
I have categories and sub-categories in a table called “categories”.
Every row has a “parent” field to note if the entry is the sub-category
of another (never goes deeper than 1 level).
So there might be something like this:
id | name | parent
Rename the ‘parent’ column to ‘parent_id’. Your model:
class Category < ActiveRecord::Base
acts_as_tree :order => :name
end
See the following URL for usage info, it’ll do all you want:
http://wiki.rubyonrails.com/rails/pages/ActsAsTree
You shouldn’t use the “sub-categories” solution suggested because you’d
have two nearly-identical models you’d have to keep in sync and other
such pain. Keep your code nice and general.
Peter H. - http://push.cx
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: If you don’t know what this is, it’s OK to ignore it.
iD8DBQFElj/sa6PWv6+ALKoRAu/UAJ9AG3Oio+jXvakpJZbrsf6VT5QGGwCeNtCk
gBx8V0Xz20Px3zyBOehWfGs=
=nZGf
-----END PGP SIGNATURE-----