简体   繁体   English

如何将国家作为整数存储在Rails中的ruby数据库中

[英]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. 我试图找出是否有一种快速的方法来使country_select存储代表国家的整数,而不是自己存储国家名称/字符串。 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]] 我认为可以做到这一点的一种方法是,如果我将gem安装为插件,然后编辑country数组,使main数组中的数组包含整数和字符串,例如COUNTRIES = [[“ United Kingdom”,1],[ “美国”,[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. 然后,我的个人资料表中可以有一个country_id列,用户选择的国家ID将存储在该列中。 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. 我将有一个单独的表“国家”,其中将存储有国家,并且我将使用配置文件表的country_id来引用国家表中的正确国家。

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. 这样做将意味着我仍将获得gem / plugin的良好功能,例如将优先级国家/地区放在选择列表的顶部。 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? 我的应用目录中的vendor文件夹对吗?

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. 我正在使用国家/地区选择gem ,它似乎仅存储字符串,而不是表示字符串的整数。

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. 如果您从country_select gem检出数组,则只有字符串。

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. 我最终从Wikipedia重新创建了国家列表,并以国家名称和iso代码作为值。 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. 更直接的方法是,我将国家/地区存储为它们的iso代码和整数,从而可以在思想狮身人面像的情况下使用该属性。

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. 然后使用虚拟属性来转换和设置值,以便您可以存储int版本而不是字符串。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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