# Prob with virtual attributes

**URL:** <https://www.ruby-forum.com/t/prob-with-virtual-attributes/116351>\
**Category:** Rails\
**Created:** [October 4, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/prob-with-virtual-attributes/116351 "2007-10-04T18:06:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [October 4, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/prob-with-virtual-attributes/116351/1 "2007-10-04T18:06:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [October 4, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/prob-with-virtual-attributes/116351/2 "2007-10-04T18:40:55Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [October 5, 2007, 6:04am UTC](https://www.ruby-forum.com/t/prob-with-virtual-attributes/116351/3 "2007-10-05T06:04:37Z")

</div>

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