ActiveRecord relationship question

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

have you looked at acts_as_tree?
http://api.rubyonrails.com/classes/ActiveRecord/Acts/Tree/ClassMethods.html

Jeff Emminger wrote:

have you looked at acts_as_tree?
http://api.rubyonrails.com/classes/ActiveRecord/Acts/Tree/ClassMethods.html

thanks,

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? %>
no items
<% else %>
there are items
<% 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

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 )