I have a relationship that I''m having a tough time dealing with and am hoping there is a way in ActiveRecord to manage it. I have a table where there is an internal relationship such as table menus id - int parent_id - int which refers to a menus id for which this is a submenu. All root menus have a parent_id of 0 and a table menu_items with the following relationship to menus table menu_items id menu_id - this is the master menu id (maps to a menus id) for which this item belongs menu_header_id - this is a key to menus id which this is a direct descendent of in other words each menu item has what is in effect two foreign keys to the same table, one to the master menu (where the menu id is 0) and the other one to which the item is a part of. is there any way for me to handle this relationship in Active Record? I had to do a lot of application level work in php for this application. -jonathan -- Posted via 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 groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
have you looked at acts_as_tree? api.rubyonrails.com/classes/ActiveRecord/Acts/Tree/ClassMethods.html --~--~---------~--~----~------------~-------~--~----~ 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 groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeff Emminger wrote:> have you looked at acts_as_tree? > api.rubyonrails.com/classes/ActiveRecord/Acts/Tree/ClassMethods.htmlthanks, that makes sense. I''m having problems loading the other "side" of the join for the foreign key. In my menu controller, I have: class MenusController < ApplicationController def show_all @menus=Menu.find:all end end and am using a partial to represent all the menu items down. This works well. I''m having a problem with the menu_items table. shouldn''t my show_all load the menu_items for each menu? I tried putting this in the view: <% if menu.menu_items.empty? %> <span>no items</span> <% else %> <span>there are items</span> <% end %> but got an "uninitialized constant MenuItem" error. It seems like it should be loading the menu_items. Is there a way to do something like print_r in php to see what is in the variable? jon -- Posted via 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 groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> but got an "uninitialized constant MenuItem" error.because you haven''t defined an actual MenuItem model class and associated it with the Menu. i''ve never gone this route, but i think the has_many :through method in the api docs uses this technique.> It seems like it should be loading the menu_items. Is there a way to do something like > print_r in php to see what is in the variable?try Marshal.dump( menu ) --~--~---------~--~----~------------~-------~--~----~ 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 groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---