Prob with virtual attributes

I currently have an activerecord object that I make available to all
my controllers through a variable called @menu that I set in
application.rb. @menu has a one to many association with it’s children
(MenuItems). @menu has a virtual attribute that I set to let it know
which menu_item is currently selected. I made it virtual just because
I don’t want the selection to be saved to the database. The problem
I’m having is that in some parts of my application I have a reference
to one of the child objects which use to get a reference to the parent
object (@menu) which, it appears, re-queries the database giving me a
reference to the saved @menu rather than the one that has the virtual
attribute set on it. How can I get around this? Thanks in advance for
any advice you can offer me. -Mike

mike wrote:

The problem
I’m having is that in some parts of my application I have a reference
to one of the child objects which use to get a reference to the parent
object (@menu) which, it appears, re-queries the database

hi there,

what you want is for

model.child.parent to return model
however, this isn’t what ActiveRecord gives you at the moment.

there’s a ticket to work around this.
http://dev.rubyonrails.org/ticket/8130

but I don’t think it’s going to get added to trunk.

My suggestion is keep display data in the display.
Chuck it into the session
session[:menu_selection][@menu.id] = @child.id

or something.

ok cool. i think this is over my head for now. thanks for your help. i
ended up doing something like what you suggested.