# Reusable State model/migration - how do you do it?

**URL:** <https://www.ruby-forum.com/t/reusable-state-model-migration-how-do-you-do-it/131838>\
**Category:** Rails\
**Created:** [March 13, 2008, 6:43pm UTC](https://www.ruby-forum.com/t/reusable-state-model-migration-how-do-you-do-it/131838 "2008-03-13T18:43:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ncancelliere](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9f8e36/32.png) [@ncancelliere](https://www.ruby-forum.com/u/ncancelliere)\
**Post date:** [March 13, 2008, 6:43pm UTC](https://www.ruby-forum.com/t/reusable-state-model-migration-how-do-you-do-it/131838/1 "2008-03-13T18:43:35Z")

</div>

I’m new to Rails. I do a lot of work that requires standard State  
FIPS codes (both alpha and numeric). I know enough on how to generate  
my model and migrations and all that. It’s a rather simple model:  
name:string, fipsalpha:string, fipsnum:integer. This lets me produce  
dropdown lists in my app that I can tie to a FIPS code. (For example  
Lousiana - LA - 22).

My question is this – is there a way to not have to populate this  
data for every Rail app I use? I know you can create a constant  
inside the model, but I’d like to keep this in the database). I was  
wondering if there was a way to have the migration in addition to  
building the schema initially populate it as well? This way I could  
use this migration in any of my projects. What’s the best way to  
handle something like this?

Nicholas

---

<div class="post-metadata">

**Author:** ![ncancelliere](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9f8e36/32.png) [@ncancelliere](https://www.ruby-forum.com/u/ncancelliere)\
**Post date:** [March 13, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/reusable-state-model-migration-how-do-you-do-it/131838/2 "2008-03-13T21:45:46Z")

</div>

Never mind. I think I figured it out. You just simply have the model  
created first, and in a later migration just do the  
Create.activerecordclass(blah).

Nicholas

---

<div class="post-metadata">

**Author:** ![ncancelliere](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9f8e36/32.png) [@ncancelliere](https://www.ruby-forum.com/u/ncancelliere)\
**Post date:** [March 14, 2008, 12:00am UTC](https://www.ruby-forum.com/t/reusable-state-model-migration-how-do-you-do-it/131838/3 "2008-03-14T00:00:59Z")

</div>

Well, you could do it all in the same migration – just after the  
table is created.

Better yet, you should create a bootstrap file that you can run as a  
rake task that will populate your tables.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![ncancelliere](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9f8e36/32.png) [@ncancelliere](https://www.ruby-forum.com/u/ncancelliere)\
**Post date:** [March 14, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/reusable-state-model-migration-how-do-you-do-it/131838/4 "2008-03-14T21:32:49Z")

</div>

This is what I ended up doing - although I’m not sure it’s the best  
solution. It seems like I should be able to use a block statement and  
iterate through a simple list to create the array.

class AddStateInfo \< ActiveRecord::Migration  
def self.up  
State.create([  
{:name =\> ‘Alaska’, :fipsalpha =\> ‘AK’, :fipsnum =\> ‘02’},  
{:name =\> ‘Alabama’, :fipsalpha =\> ‘AL’, :fipsnum =\> ‘01’},  
{:name =\> ‘Arkansas’, :fipsalpha =\> ‘AR’, :fipsnum =\> ‘05’},  
… etc, etc…  
])  
end

def self.down  
State.delete\_all  
end  
end

On Mar 13, 6:00 pm, “removed\_email\_address@domain.invalid” \<ESPN3.DL-

---

<div class="post-metadata">

**Author:** ![ncancelliere](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9f8e36/32.png) [@ncancelliere](https://www.ruby-forum.com/u/ncancelliere)\
**Post date:** [April 4, 2008, 8:45pm UTC](https://www.ruby-forum.com/t/reusable-state-model-migration-how-do-you-do-it/131838/5 "2008-04-04T20:45:16Z")

</div>

Actually I agree the better way is to model this in a fixture (csv in  
this case) and then I can load it and equally use it for testing  
later. Thanks for the suggestion - it’s worked out great!

Nicholas
