Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 405643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:25:09+00:00 2026-05-12T17:25:09+00:00

I want to make a language selection dropdown in a site user edit/create page.

  • 0

I want to make a language selection dropdown in a site user edit/create page.

For this purpose, I have of course translated the site to more than one language.
Using I18n.available_languages, I can then get an array of locale codes, like so

development environment (Rails 2.3.4)
> I18n.available_locales
   => [:en, :da]

Furthermore, I have created a Language model and related it to User:

# app/models/language.rb
class Language < ActiveRecord::Base
  has_many :users  
end

# app/models/user.rb
class User < ActiveRecord::Base
  belongs_to :language  
end

# db/schema.rb
create_table "languages", :force => true do |t|
  t.string "name"
  t.string "code"
end

create_table "users", :force => true do |t|
  t.integer  "language_id"
end

The language table then contains a locale code and a language name in the native tongue, like so:

| id  | name                | code |
------------------------------------
| 28  | Dansk               | da   |
| 29  | Nederlands          | nl   |
| 30  | English             | en   |
| 31  | Esperanto           | eo   |

I then have the following assignment in the User new, create and edit actions:

# app/controllers/users_controller.rb (extract)
@available_languages = I18n.available_locales.collect {|language_code| Language.find_by_code(language_code.to_s)}

which I use in the view like so (‘available_languages’ is a local variable, since @available_languages from the controller has been passed to a partial):

# app/views/users/_form.haml (extract)
= f.collection_select(:language_id, available_languages, :id, :name, {:prompt => true})

The upshot of all this, is that the user will get a locale select dropdown to define the locale for the given user.

My question is:
Is there a clean way to move the @available_languages assignment out of the UsersController and into the Language model, so I can shorten this:

@available_languages = I18n.available_locales.collect {|language_code| Language.find_by_code(language_code.to_s)}

to something like this:

@available_languages = Language.translations_available
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:25:09+00:00Added an answer on May 12, 2026 at 5:25 pm

    It seems to me that you’re doing a couple of funny things. First off there are some problems with the following:

    I18n.available_locales.collect {|language_code| Language.find_by_code(language_code.to_s)}
    

    This setup causes you to generate one SQL query for every available locale. Furthermore if every locale in I18n.available_locales has a corresponding Language object and vice-versa, this code seems a bit unnecessary. You might as well just do:

    Language.find(:all) # or even Language.all
    

    If for some reason, they don’t map directly, you could use this instead:

    Language.all(:conditions => { :code => I18n.available_locales })
    

    which in a more verbose form is equivalent to:

    Language.find(:all, :conditions => ["code IN (?)", I18n.available_locales])
    

    This will find all languages whose code is listed in I18n.available_locales. If you want a shortcut to this method, you can use named_scopes:

    class Language < ActiveRecord::Base
      has_many :users
    
      named_scope :translation_available, :conditions => { :code => I18n.available_locales }
    end
    

    With this, you can then call:

    Language.translation_available
    

    I think this is what you wanted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.