# Order that virtual attributes are processed

**URL:** <https://www.ruby-forum.com/t/order-that-virtual-attributes-are-processed/148441>\
**Category:** Rails\
**Created:** [September 12, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/order-that-virtual-attributes-are-processed/148441 "2008-09-12T21:50:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 12, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/order-that-virtual-attributes-are-processed/148441/1 "2008-09-12T21:50:02Z")

</div>

hello

I have a nested resource that needs the id of its parent when it is  
being created. This parent id is stored in a hidden attribute in the  
form (we’ll call this attribute A). I need this parent id to process the  
data from another virtual attribute on the same form (attribute B).

Currently, attribute B is being processed in the model before attribute  
a, which is in the opposite way that I need it. So is there a way that  
attribute A can be processed before attribute B? I have tried  
rearranging the code putting attribute A’s method above B’s and it  
hasn’t work.

Or is there a better way to discover the parent id before the child is  
saved?

Thanks,  
Mike

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 13, 2008, 10:26am UTC](https://www.ruby-forum.com/t/order-that-virtual-attributes-are-processed/148441/2 "2008-09-13T10:26:40Z")

</div>

On Sep 12, 8:50 pm, Michael Lavrisha \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> rearranging the code putting attribute A’s method above B’s and it  
> hasn’t work.

I take it that you’re just passing a hash to update\_attributes. You  
can’t rely on a hash being in any particular order. Your best bet is  
to pull out of the hash the value that needs to be set first and set  
it yourself.

Fred

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 13, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/order-that-virtual-attributes-are-processed/148441/3 "2008-09-13T22:30:13Z")

</div>

Frederick C. wrote:

> On Sep 12, 8:50ï¿½pm, Michael Lavrisha \<rails-mailing-l…@andreas-  
> Your best bet is  
> to pull out of the hash the value that needs to be set first and set  
> it yourself.
> 
> Fred

How do you access the hash from the form? I know you can just get it  
from params in the controller but I don’t know how to access it in the  
model.

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 13, 2008, 11:04pm UTC](https://www.ruby-forum.com/t/order-that-virtual-attributes-are-processed/148441/4 "2008-09-13T23:04:32Z")

</div>

On 13 Sep 2008, at 21:30, Michael Lavrisha wrote:

> from params in the controller but I don’t know how to access it in the  
> model.

I was thinking along the lines of

def some\_action  
@record = Record.find[:id]  
@record.some\_attribute = params[:record].delete :some\_attribute  
@record.update\_attributes! params[:record]  
end

Fred

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 14, 2008, 2:22am UTC](https://www.ruby-forum.com/t/order-that-virtual-attributes-are-processed/148441/5 "2008-09-14T02:22:39Z")

</div>

Frederick C. wrote:

> On 13 Sep 2008, at 21:30, Michael Lavrisha wrote:
> 
> > from params in the controller but I don’t know how to access it in the  
> > model.
> 
> I was thinking along the lines of
> 
> def some\_action  
> @record = Record.find[:id]  
> @record.some\_attribute = params[:record].delete :some\_attribute  
> @record.update\_attributes! params[:record]  
> end
> 
> Fred

Yeah, that is essentially what I want to do but I don’t know the syntax  
to do it.

So in my view I have:  
translations/new.html.erb  
\<%= hidden\_field\_tag ‘translation[project]’, params[:project\_id] %\>

Then in the model:  
translation.rb  
class Translation \< ActiveRecord::Base  
belongs\_to :project

def attribute\_a=(project)  
@project = project.id  
end

def attribute\_b=(attr\_b)

# this is where I need @project or the attribute\_a value

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [September 14, 2008, 11:07am UTC](https://www.ruby-forum.com/t/order-that-virtual-attributes-are-processed/148441/6 "2008-09-14T11:07:44Z")

</div>

On Sep 14, 1:22 am, Michael Lavrisha \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> > @record.some\_attribute = params[:record].delete :some\_attribute  
> > @record.update\_attributes! params[:record]  
> > end
> 
> > Fred
> 
> Yeah, that is essentially what I want to do but I don’t know the syntax  
> to do it.

I’ve just shown you how to do it 🙂 Replace :record with :translation  
and :some\_attribute with :project and you’ll be on your way (you just  
don’t need the .id since what you’re getting is an id already).  
However I’m confused why you have this accessor at all. Sure the  
project\_id value should just be stored in the project\_id column ?

Fred
