I''m fairly new to Ruby on Rails, and am having trouble figuring out how to do this. I have a model called Organization. I want to set things up so that an org can have sub-orgs (which can have sub-orgs themselves). Can anyone point me to a good example or explanation (or provide one directly) so I can get an idea of how to do this? Thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I''ve just been doing this myself with systems and sub-systems.
In the system model I have:
has_many :sub_systems, :class_name => "System", :foreign_key
=>
"system_id", :dependent=>:nullify
Then you can refer to the system.sub_systems
I also have code to prevent loops which will go around forever if you
traverse the system-sub-system tree.
def validate
errors.add(:sub_systems, "Cannot be a subsystem of itself.") if
all_subs.include?(self)
end
def all_subs(subs = [])
self.sub_systems.each do |s|
unless subs.include?(s)
subs << s
s.all_subs(subs)
end
end
subs
end
Hope that helps.
On Oct 8, 10:53 am, David Hill
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> I''m fairly new to Ruby on Rails, and am having trouble figuring
out how
> to do this. I have a model called Organization. I want to set things
> up so that an org can have sub-orgs (which can have sub-orgs
> themselves).
>
> Can anyone point me to a good example or explanation (or provide one
> directly) so I can get an idea of how to do this?
>
> Thanks!
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Look into the :as and :source options for the has_many and belongs_to associations, they should be able to do what you need. Good luck! On Oct 7, 5:53 pm, David Hill <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I''m fairly new to Ruby on Rails, and am having trouble figuring out how > to do this. I have a model called Organization. I want to set things > up so that an org can have sub-orgs (which can have sub-orgs > themselves). > > Can anyone point me to a good example or explanation (or provide one > directly) so I can get an idea of how to do this? > > Thanks! > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I forgot this - you also have to have belongs_to :system On Oct 8, 11:10 am, "stuart.coyle" <stuart.co...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve just been doing this myself with systems and sub-systems. > > In the system model I have: > > has_many :sub_systems, :class_name => "System", :foreign_key => > "system_id", :dependent=>:nullify > > Then you can refer to the system.sub_systems > > I also have code to prevent loops which will go around forever if you > traverse the system-sub-system tree. > > def validate > errors.add(:sub_systems, "Cannot be a subsystem of itself.") if > all_subs.include?(self) > end > > def all_subs(subs = []) > self.sub_systems.each do |s| > unless subs.include?(s) > subs << s > s.all_subs(subs) > end > end > subs > end > > Hope that helps. > > On Oct 8, 10:53 am, David Hill <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > I''m fairly new to Ruby on Rails, and am having trouble figuring out how > > to do this. I have a model called Organization. I want to set things > > up so that an org can have sub-orgs (which can have sub-orgs > > themselves). > > > Can anyone point me to a good example or explanation (or provide one > > directly) so I can get an idea of how to do this? > > > Thanks! > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Acts as Tree: http://wiki.rubyonrails.org/rails/pages/ActsAsTree Does everything you need. On Oct 7, 8:53 pm, David Hill <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I''m fairly new to Ruby on Rails, and am having trouble figuring out how > to do this. I have a model called Organization. I want to set things > up so that an org can have sub-orgs (which can have sub-orgs > themselves). > > Can anyone point me to a good example or explanation (or provide one > directly) so I can get an idea of how to do this? > > Thanks! > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---