Search code examples
rubyobjectruby-on-rails-3.1

create new object in rails


I can't find a good example of this, so please point me in the right direction.

I want to create an object from scratch with 2 attributes abbr and name

I am trying to make an object with the 50 states and DC. Since I don't see this list changing every often, I don't see a need for a database, maybe I am wrong.

I have tried the following without success:

new_state = Object.new        
new_state.abbr = state[:abbr]     
new_state.name = state[:name]

and I get undefined method abbr=' for #<Object:0x00000006763008>

What am I doing wrong?


Solution

  • you can use decoder that reads from/keeps its states/abbreviations/i18n in YAML.

    Decoder.i18n = :en
    country = Decoder::Countries[:US]
    country.to_s
    # => "United States"    
    
    state = country[:MA]
    state.to_s
    # => "Massachusetts"