Hi! I have a problem with belongs_to and has_many, the thing is i have a category table which points to herself if it''s a subcategory of another category. I just started using ruby on rails, and don''t know how to implement that or if it can be done (I assume it can). Thank for any help. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 28, 2:17 pm, Adam <anlauf.a...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi! > I have a problem with belongs_to and has_many, the thing is > i have a category table which points to herself if it''s a subcategory > of another category. > > I just started using ruby on rails, and don''t know how to implement > that or if it can be done (I assume it can).You assume correctly. Here''s an example: class Category < ActiveRecord::base belongs_to :parent_category, :class_name => ''Category'', :foreign_key => ''parent_category_id'' has_many :subcategories, :class_name => ''Category'', :foreign_key => ''parent_category_id'' end Your migration might contain this (among other stuff): create_table :categories do |t| t.column :parent_category, :integer end And now you can theoretically do things like: subs = Category.find(1).subcategories parent = Category.find(2).parent -Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 28, 2:49 pm, Bill Kocik <bko...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Your migration might contain this (among other stuff): > > create_table :categories do |t| > t.column :parent_category, :integer > endWoops - that, of course, should have been: t.column :parent_category_id, :integer My bad. -Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 28, 2:49 pm, Bill Kocik <bko...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> And now you can theoretically do things like: > > subs = Category.find(1).subcategories > parent = Category.find(2).parentAnd this should have been: parent = Category.find(2).parent_category Sorry for the extra messages. I wasn''t being as careful and thorough as I should have been. -Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Great!! Thanks for a quick answer! On 28 Gru, 20:55, Bill Kocik <bko...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Dec 28, 2:49 pm, Bill Kocik <bko...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > And now you can theoretically do things like: > > > subs = Category.find(1).subcategories > > parent = Category.find(2).parent > > And this should have been: > > parent = Category.find(2).parent_category > > Sorry for the extra messages. I wasn''t being as careful and thorough > as I should have been. > > -Bill--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Quoting Adam <anlauf.adam-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > Hi! > I have a problem with belongs_to and has_many, the thing is > i have a category table which points to herself if it''s a subcategory > of another category. > > I just started using ruby on rails, and don''t know how to implement > that or if it can be done (I assume it can). >Look at acts_as_tree. It''s a plugin in Rails 2.0, builtin in Rails 1.2. Example: class Task < ActiveRecord::Base has_many :comments, :dependent=>:destroy end class Comment < ActiveRecord::Base belongs_to :task acts_as_tree :order=>''created_at'' end Tasks have a hierarchy of comments with replies and replies can have replies arbitrarily deep. task = Task.find(id) task.comments # top level comments task.comments[0].comments # replies to first top level comment comment is like a category, reply is like a sub-category. HTH, Jeffrey --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
OK, thanks!! I''ll look that up too. On 29 Gru, 06:44, "Jeffrey L. Taylor" <r...-NvlhjL+mh2pvqUDvg8NhqHL8HoS0Hn3T@public.gmane.org> wrote:> Quoting Adam <anlauf.a...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > Hi! > > I have a problem with belongs_to and has_many, the thing is > > i have a category table which points to herself if it''s a subcategory > > of another category. > > > I just started using ruby on rails, and don''t know how to implement > > that or if it can be done (I assume it can). > > Look at acts_as_tree. It''s a plugin in Rails 2.0, builtin in Rails 1.2. > > Example: > > class Task < ActiveRecord::Base > has_many :comments, :dependent=>:destroy > end > > class Comment < ActiveRecord::Base > belongs_to :task > acts_as_tree :order=>''created_at'' > end > > Tasks have a hierarchy of comments with replies and replies can have replies > arbitrarily deep. > > task = Task.find(id) > task.comments # top level comments > task.comments[0].comments # replies to first top level comment > > comment is like a category, reply is like a sub-category. > > HTH, > Jeffrey--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---