Order that virtual attributes are processed

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

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.

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

Frederick Cheung 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.

Frederick Cheung 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.

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

Frederick Cheung 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

Frederick Cheung 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.

I've just shown you how to do it :slight_smile: 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