Ran into a little butt biter today… I imported a few thousand addresses that had the state in long form, like ‘Arizona’. But I commonly used the us_states plugin for popup selection of states. Well, us_states will be looking for state abbreviations, like ‘AZ’ when it selects it’s selected value. So what’s a girl (or dude) to do? You gotta convert them states!
But there are a few concerns:
- you need to iterate over a large set of records, maybe an entire table
- there may be memory issues with loading such a large set
The following rake task demonstrates a technique that is more memory efficient than loading the entire model/attributes (table/fields) into memory. Only load the ‘id’ and ‘state’ attributes when you need to find your initial record set. The ‘id’ will be used to find the record for updating, and the state is need to find the new abbreviation.