Self refrencing many to one implementation

i have database for dynamic website in which i need to store
information about each menu and its relationship with other menu.
it is obvious that each menu can have several submenu but each submenu
must belong to one and ony one menu.
so database goes like this

title =>integer, it is name of menu
Image_path=>string, path of image
Content_text=> text contained on click of every menu or description
fK_parent_content_id => integer , THIS LINKS BACK TO pk_content_id ,as
is a foreign key

what i want is to relate pk_content_id and fk_parent_content_id (which
are in same table)

how to accomplish such self refrencing many to one relation???

m extreme beginner in rails please be more descriptive.

just to mention pk- represents primary key so is unique while fk is
foreign key and is not unique, combinaly forms one to many relation

thanks in advance

On 21 February 2010 05:50, mac [email protected] wrote:


The term you’re looking for is “self referential” - very handy when
Authors have Readers, or Employees have Managers…

You’ll find lots of tutorials online, but essentially you set it up

Menu model

has_many :children, :class_name => “Menu”, :foreign_key =>
“parent_content_id” # those ‘pk/fk’ prefixes are not really good from
the Rails conventions
belongs_to :parent, , :class_name => “Menu”

But you might find that one of the “nesting” algorithms suits your
need better - seeing as it seems you might be constantly recurring
down the menus, “awesome_nested_set” might be worth a look.

Hope this helps put you on track.