Current value of object.method in helper

Hi,

I am currently working to generate selection from my validation table
where code and description pairs are stored. This works fine except
when the current value is not exist in validation table. There are
some old data which contains values that are no longer used and not in
my validation table. I do not want to add old values in my validation
table; however, I would like to display these when user open a form.

In order to accomplish this requirement, I need to dynamically add a
current value of the record to selections in view helper like
following.

def validation_select(object, method, options = {}, html_options =
{})
selections = Validation.find(:all).map {|v| [v.description,
v.code] }
selections.push [object[method], object[method]] #This does not
work!
select(object, method, selections, options, html_options)
end

Names of object and method are passed in. How do I get current value
of object? object[method] does not work because it is just a string
not a real object.

Say if have a model called Person which has a field called
“ethnicity”.
There is an instance @person, and I will call
validation_select(‘person’, ‘ethnicity) in my view’.
Is there any way, I can get the current value of @person.ethnicity in
view helper?

Thanks

Glenn

On 2007-10-27 15:26:12 -0700, Glenn
[email protected] said:

Say if have a model called Person which has a field called
“ethnicity”.
There is an instance @person, and I will call
validation_select(‘person’, ‘ethnicity) in my view’.
Is there any way, I can get the current value of @person.ethnicity in
view helper?

def validation_select(object, method, … )
model = object.classify.constantize
new_option = [ method, model.send(method) ]
end

I think this is what you’re looking for if I understand your Q?

–Andrew V.

Hi Andrew,

Thanks you so much for your help!
It seems “constantize” is the one I need to look into.
I love Ruby’s strong reflection features.

Glen