简体   繁体   中英

How to store countries as integers in a database in ruby on rails

What I'm trying to do:

I'm trying to find out if there is a quick way to get country_select to store integers representing the countries instead of storing the country names/strings themselves. I'm using the country select gem .

Possible solution:

One way I'm thinking this can be done is if I install the gem as a plugin and edit the countries array to have arrays within the main array with integers and a string eg COUNTRIES = [["United Kingdom", 1],["United States", 2]]

This way in my form there will be values representing the strings. Then I can have a country_id column in my profiles table where users selected countries id will be stored. I will have a separate table "countries" that will have the countries stored in it and I'll use the country_id of the profiles table to reference the correct country in the countries table.

Doing it this way would mean I would still get the good features of the gem/plugin such as having priority countries at the top of the select list. Something I don't know how to do on my own.

It would take long but it could work. If I chose this solution where would I put the plugin? vendors folder in my apps directory right?

But if there is a quicker way to do this I'd like to do it that way.

A bigger picture:

Ok I have a search form where a user can browse the sites users filtering out results by:

text typed location gender sexuality marital status country

I'm using thinking sphinx and when filtering attributes it seems that the attributes need to be represented integers because everything works but the country.

I'm using the country select gem and it seems to only store strings and not an integer representing the string.

I would like to have it store integers instead.

Here are some contants I use in my search forms:

module ApplicationHelper

    GENDER            = [["Select", nil],["Male", 1],["Female", 2]]

    ETHNICITY         = [["Select", nil],['Black', 1 ],['White / Caucasian', 2 ],['European', 3 ],['Asian', 4 ],
                         ['Indian', 5 ],['Middle Eastern', 6 ],['Native American', 7 ],['Hispanic', 8 ],
                         ['Mixed Race', 9 ],['Other Ethnicity', 10 ]]

    MARITAL_STATUS    = [[' Select', nil],['Single', 1 ],['Dating', 2 ],['In relationship', 3 ],['Married', 4 ],
                         ['Living Together', 5 ],['Divorced', 6 ],['Separated', 7 ],['Widowed', 8 ]]

    SEXUAL_PREFERENCE = [[' Select', nil],['Straight', 1 ],['Gay', 2 ],['Bi-sexual', 3 ]]

end

The search/browse form:

<%= form_tag browsers_path, :method => 'get' do %>
  <p>
    Location: <%= text_field_tag :location, params[:location] %>
<br />
    Country: <%= country_select :country, [ "United Kingdom", "France", "Germany" ] %>
<br />
    Gender: <%= select_tag :gender, options_for_select(ApplicationHelper::GENDER, params[:gender]) %>
<br />
    Ethnicity: <%= select_tag :ethnicity, options_for_select(ApplicationHelper::ETHNICITY, params[:ethnicity]) %>
<br />

    Marital status: <%= select_tag :marital_status, options_for_select(ApplicationHelper::MARITAL_STATUS, params[:marital_status]) %>
<br />
    Sexual preference: <%= select_tag :sexual_preference, options_for_select(ApplicationHelper::SEXUAL_PREFERENCE, params[:sexual_preference]) %>
<br />
    <%= submit_tag "Search", :name => nil %>
  </p>
  <% end %>

as you can see each array has a string and an integer. If you check out the array from the country_select gem there are just strings.

I'd appreciate an explanation of the best possible way to do what I'm trying to do and a clear example if possible.

I hope this post made sense.

Kind regards

I ended up recreating the country list from wikipedia with name of country and iso code as value. Much more straight forward and I got to store countries as their iso code and as integers making it possible to use the attribute with thinking sphinx.

Create the constant like you have for the other filters. Then use a virtual attribute to translate and set the values so that you can store the int version and not the string.

EDIT:

Maybe I do not understand what we are talking about exactly but if you are trying to store the value in a model somewhere you can do something like this:

def country_int(country_s)
  country = COUNTRY[country_s]
end

def country_int
  COUNTRY.key(country)
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM