Every time we need this migration script for our projects
Use following command for Model generate
[siwan@localhost siwan]$ ruby script/generate model UsStates
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/us_states.rb
create test/unit/us_states_test.rb
create test/fixtures/us_states.yml
exists db/migrate
create db/migrate/20091117115011_create_us_states.rb
[siwan@localhost siwan]$
Open the /db/migrate/20091117115011_create_us_states.rb file and paste following code:
class CreateUsStates ‘Alabama’, :abbreviation => ‘AL’
UsStates.create :name => ‘Alaska’, :abbreviation => ‘AK’
UsStates.create :name => ‘Arizona’, :abbreviation => ‘AZ’
UsStates.create :name => ‘Arkansas’, :abbreviation => ‘AR’
UsStates.create :name => ‘California’, :abbreviation => ‘CA’
UsStates.create :name => ‘Colorado’, :abbreviation => ‘CO’
UsStates.create :name => ‘Connecticut’, :abbreviation => ‘CT’
UsStates.create :name => ‘Delaware’, :abbreviation => ‘DE’
UsStates.create :name => ‘District of Columbia’, :abbreviation => ‘DC’
UsStates.create :name => ‘Florida’, :abbreviation => ‘FL’
UsStates.create :name => ‘Georgia’, :abbreviation => ‘GA’
UsStates.create :name => ‘Hawaii’, :abbreviation => ‘HI’
UsStates.create :name => ‘Idaho’, :abbreviation => ‘ID’
UsStates.create :name => ‘Illinois’, :abbreviation => ‘IL’
UsStates.create :name => ‘Indiana’, :abbreviation => ‘IN’
UsStates.create :name => ‘Iowa’, :abbreviation => ‘IA’
UsStates.create :name => ‘Kansas’, :abbreviation => ‘KS’
UsStates.create :name => ‘Kentucky’, :abbreviation => ‘KY’
UsStates.create :name => ‘Louisiana’, :abbreviation => ‘LA’
UsStates.create :name => ‘Maine’, :abbreviation => ‘ME’
UsStates.create :name => ‘Maryland’, :abbreviation => ‘MD’
UsStates.create :name => ‘Massachutsetts’, :abbreviation => ‘MA’
UsStates.create :name => ‘Michigan’, :abbreviation => ‘MI’
UsStates.create :name => ‘Minnesota’, :abbreviation => ‘MN’
UsStates.create :name => ‘Mississippi’, :abbreviation => ‘MS’
UsStates.create :name => ‘Missouri’, :abbreviation => ‘MO’
UsStates.create :name => ‘Montana’, :abbreviation => ‘MT’
UsStates.create :name => ‘Nebraska’, :abbreviation => ‘NE’
UsStates.create :name => ‘Nevada’, :abbreviation => ‘NV’
UsStates.create :name => ‘New Hampshire’, :abbreviation => ‘NH’
UsStates.create :name => ‘New Jersey’, :abbreviation => ‘NJ’
UsStates.create :name => ‘New Mexico’, :abbreviation => ‘NM’
UsStates.create :name => ‘New York’, :abbreviation => ‘NY’
UsStates.create :name => ‘North Carolina’, :abbreviation => ‘NC’
UsStates.create :name => ‘North Dakota’, :abbreviation => ‘ND’
UsStates.create :name => ‘Ohio’, :abbreviation => ‘OH’
UsStates.create :name => ‘Oklahoma’, :abbreviation => ‘OK’
UsStates.create :name => ‘Oregon’, :abbreviation => ‘OR’
UsStates.create :name => ‘Pennsylvania’, :abbreviation => ‘PA’
UsStates.create :name => ‘Rhode Island’, :abbreviation => ‘RI’
UsStates.create :name => ‘South Carolina’, :abbreviation => ‘SC’
UsStates.create :name => ‘South Dakota’, :abbreviation => ‘SD’
UsStates.create :name => ‘Tennessee’, :abbreviation => ‘TN’
UsStates.create :name => ‘Texas’, :abbreviation => ‘TX’
UsStates.create :name => ‘Utah’, :abbreviation => ‘UT’
UsStates.create :name => ‘Vermont’, :abbreviation => ‘VT’
UsStates.create :name => ‘Virginia’, :abbreviation => ‘VA’
UsStates.create :name => ‘Washington’, :abbreviation => ‘WA’
UsStates.create :name => ‘West Virginia’, :abbreviation => ‘WV’
UsStates.create :name => ‘Wisconsin’, :abbreviation => ‘WI’
UsStates.create :name => ‘Wyoming’, :abbreviation => ‘WY’
end
def self.down
drop_table :us_states
end
end
Goodlunk, This code will save your problem of fetching USA state names and abbreviations as per id
Check my controller code…Just put in your controller
@us_states = USStates.find(:all)
<% form_for :customer, @customer, :url => { :action => “new_application” } do |f| %>
<%= f.select :state_id, @us_states.collect {|state| [ state.abbreviation, state.id ] } %>
<% end %>