# Populating and maintaining an instance array of arrays of hashes over multiple Ajax calls

**URL:** <https://www.ruby-forum.com/t/populating-and-maintaining-an-instance-array-of-arrays-of-hashes-over-multiple-ajax-calls/157183>\
**Category:** Rails\
**Created:** [January 8, 2009, 7:51am UTC](https://www.ruby-forum.com/t/populating-and-maintaining-an-instance-array-of-arrays-of-hashes-over-multiple-ajax-calls/157183 "2009-01-08T07:51:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@ram](https://www.ruby-forum.com/u/ram)\
**Post date:** [January 8, 2009, 7:51am UTC](https://www.ruby-forum.com/t/populating-and-maintaining-an-instance-array-of-arrays-of-hashes-over-multiple-ajax-calls/157183/1 "2009-01-08T07:51:17Z")

</div>

Hi,  
I have a Currency model with id, name and exchange\_rate attributes.  
I have multiple select boxes for currencies in a form. These can be  
added dynamically through JS too.  
Im using an observe\_field to make an Ajax call to the action  
“currency\_name” and retrieve the selected currency from the database  
using the id.

Following this Ajax call, currency\_name.rjs is rendered where i update  
the “selected\_currencies” div with the currency name and amount(which  
is the exchange\_rate \* some\_value). In case a previously chosen  
currency is chosen again, only the amount should get updated. But if a  
new currency is chosen, “selected\_currencies” should get appended with  
the new currency’s name and amount.

To accomplish this condition checking, I thought of having an instance  
@selected\_taxes which is an array of arrays (like  
[[“currency1\_id”,“currency1\_name”,“currency1\_rate”],  
[“currency2\_id”,“currency2\_name”,“currency2\_rate”] , …] ) OR an  
array of arrays of hashes (which carry the appropriate keys for the  
values). But this array seems to refresh on every Ajax call. I’ve  
paste the controller code below

def currency\_name  
@chosen\_currencies ||= []  
flag = 0  
@currency = Currency.find(params[:currency\_id])  
@chosen\_currencies.each do |id|  
if (@currency.id == id)  
flag = 1  
end  
end  
@chosen\_currencies \<\< @currency.id unless (flag == 1)  
raise @chosen\_currencies.to\_yaml  
end

This results in the instance having only one ID at a time.  
Firstly, is my approach correct? Or is there a better way?  
And secondly, if my approach is correct, how can i maintain this  
instance over multiple Ajax calls?  
Hope someone can help me out with this.
