views:

163

answers:

1

I am new to Rails. I have following construction. Mainmenu in my project has many submenus. In defining @submenu is everything ok, but in finding @mainmenu not. The output is: Couldn't find Mainmenu without an ID

@submenu = Submenu.find(params[:submenu_id])
@mainmenu = Mainmenu.find(params[:id => @submenu.mainmenu_id])

How can i define @mainmenu so it is submenu`s upperclass?

I have Rails 2.

A: 

Rails allows you to retrieve relations just like that :

@mainmenu = @submenu.mainmenu

But if you wanted (for some obscure reason or in an other context), to retrieve the menu like you did before, you should do :

@mainmenu = Mainmenu.find_by_id(@submenu.mainmenu_id)

You might want to read this : Active Record Query Interface in order to know how to appropriately do SQL queries with Active Record.

Damien MATHIEU
So easy... Thank`s! You made my day!
Yurish