Variable to find a function in rails 2
I am new to Rails. I have the following construction. The mainmenu in my project has many submenus. While defining @submenu everything is fine, but searching for @mainmenu doesn't. Conclusion: Failed to find Mainmenu without ID
@submenu = Submenu.find(params[:submenu_id])
@mainmenu = Mainmenu.find(params[:id => @submenu.mainmenu_id])
How can I define @mainmenu so that this is the top class of the submenu?
I have Rails 2.
+2
source to share
1 answer
Rails allows you to restore relationships like this:
@mainmenu = @submenu.mainmenu
But if you want (for some obscure reason or in a different context) to get the menu like you did before, you need to do:
@mainmenu = Mainmenu.find_by_id(@submenu.mainmenu_id)
You can read the following: Active Request Query Interface to learn how to properly execute SQL queries using Active Record.
+1
source to share