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 6211549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:21:28+00:00 2026-05-24T06:21:28+00:00

Currently, I have an action in my customers controller generating an array of names,

  • 0

Currently, I have an action in my customers controller generating an array of names, @username_array, of all objects of class User with which to populate a drop down menu in a form that creates a new object of class Customer. The form element looks like this right now:

<%= f.select :user_id, @username_array %>

What I’d really like is for the id of the user to be sent into params[:customer][:user_id] instead of the name of that user that is chosen in the drop down. So in my create action in my customers controller I have the following code:

@customer = Customer.new(params[:customer])
@user = User.find_by_name(params[:customer][:user_id]) # where :user_id is actually currently the name selected from the drop down
@customer.user_id = @user.id

Is there an easier way of doing this?

  • 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-24T06:21:28+00:00Added an answer on May 24, 2026 at 6:21 am

    Change your @username_array to include both the name and the id of the user:

    Instead of:

    ["Bob","Sally","Dave"]
    

    Make it:

    [["Bob", 1],["Sally",2],["Dave",3]]
    

    This could be accomplished by something like this:

    @username_array = User.all.map {|user| [user.name, user.id]}
    

    Then, f.select will display the name in the dropdown, but the actual value passed in through params[:customer][:user_id] will be the id of the user, which is what you want. With this in place, the following is all you need in the controller action code:

    @customer = Customer.new(params[:customer])
    

    You won’t have to look up the user by name, the params hash will already have the correct id value.

    Note that instead of making @username_array an instance variable you could just create a utility method in the helper for this controller, or the application helper:

    def user_select
      User.all.map {|user| [user.name, user.id]}
    end
    

    Then, in your view:

    <%= f.select :user_id, user_select %>
    

    If you put this in your application helper, you can use it everywhere and only have the code in one place (DRY).

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

Sidebar

Related Questions

I currently have a simple MVC RedirectToAction action: return RedirectToAction(Edit, Customers, new {Id =
I currently have some action associated with when the user types something in a
I have a custom validation attribute derived from action filter attribute. Currently the attribute
I currently have a class and I'm trying to create an easy GUI to
I am developing an application with the Zend Frameworks. I currently have the Controller
I currently have code in my ApplicationController to check if a user is logged
<Custom Action=SetARPINSTALLLOCATION After=InstallValidate>NOT (REMOVE=ALL or REMOVE=ProgramFiles)</Custom> I have this custom action called in InstallSequence,
I have an Auction model with several attributes, two of which are current_auction:boolean and
I currently have an MS Access application that connects to a PostgreSQL database via
I currently have speakers set up both in my office and in my living

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.