Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to store countries as integers in a database in ruby on rails
    text
    copied!<p><strong>What I'm trying to do:</strong></p> <p>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 <a href="https://github.com/jamesds/country-select" rel="nofollow">country select gem</a>.</p> <p><strong>Possible solution:</strong></p> <p>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 e.g. COUNTRIES = [["United Kingdom", 1],["United States", 2]] </p> <p>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.</p> <p>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.</p> <p>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?</p> <p>But if there is a quicker way to do this I'd like to do it that way.</p> <p><strong>A bigger picture:</strong></p> <p>Ok I have a search form where a user can browse the sites users filtering out results by:</p> <p>text typed location gender sexuality marital status country</p> <p>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.</p> <p>I'm using the <a href="https://github.com/jamesds/country-select" rel="nofollow">country select gem</a> and it seems to only store strings and not an integer representing the string.</p> <p>I would like to have it store integers instead.</p> <p><strong>Here are some contants I use in my search forms:</strong></p> <pre><code>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 </code></pre> <p><strong>The search/browse form:</strong></p> <pre><code>&lt;%= form_tag browsers_path, :method =&gt; 'get' do %&gt; &lt;p&gt; Location: &lt;%= text_field_tag :location, params[:location] %&gt; &lt;br /&gt; Country: &lt;%= country_select :country, [ "United Kingdom", "France", "Germany" ] %&gt; &lt;br /&gt; Gender: &lt;%= select_tag :gender, options_for_select(ApplicationHelper::GENDER, params[:gender]) %&gt; &lt;br /&gt; Ethnicity: &lt;%= select_tag :ethnicity, options_for_select(ApplicationHelper::ETHNICITY, params[:ethnicity]) %&gt; &lt;br /&gt; Marital status: &lt;%= select_tag :marital_status, options_for_select(ApplicationHelper::MARITAL_STATUS, params[:marital_status]) %&gt; &lt;br /&gt; Sexual preference: &lt;%= select_tag :sexual_preference, options_for_select(ApplicationHelper::SEXUAL_PREFERENCE, params[:sexual_preference]) %&gt; &lt;br /&gt; &lt;%= submit_tag "Search", :name =&gt; nil %&gt; &lt;/p&gt; &lt;% end %&gt; </code></pre> <p>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.</p> <p>I'd appreciate an explanation of the best possible way to do what I'm trying to do and a clear example if possible.</p> <p>I hope this post made sense.</p> <p>Kind regards</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload