Hi, This might be a simple prob. But I am new to rails. I am trying to convert a string value into a name of an attribute that belongs to an object. For example, in the following code, I need all the string values in the column_array turned into attribute names. "student_identification" , "email".. all these are actual column names of my 'Student' table. In the real scenario, 'column_array' will be set by the user (by ticking check boxes). And 'new_array' will be replaced by 'csv', as I want the data go into a csv file.
At the moment I am really struggling at the following line:
new_array << r."#{column_array[i]}"
I want '{column_array[i]}' to be turned into the attribute name so i can access the data.
def exp_tst
@records = Student.find(:all, :conditions=> session[:selection_scope],
:order => sort_order('laboratory_id'))
column_array = ["student_identification", "laboratory_id", "email", "current_status"]
new_array = Array.new()
@records.each do |r|
(0..(column_array.size-1)).each do |i|
new_array << r."#{column_array[i]}"
end
end
end
any help or suggestions will be greatly appreciated. Thanks.