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

  • SEARCH
  • Home
  • 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 6099647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:17:48+00:00 2026-05-23T13:17:48+00:00

I currently have a .find method in one of my rails controller actions –

  • 0

I currently have a .find method in one of my rails controller actions – the relevant part is:

.find(:all, :select => 'last_name as id, last_name as name')

I am getting some odd behaviour trying to alias the last_name column as id – if I alias it as anything else, it works fine (i can do last_name as xyz and it outputs the last name in a column called xyz, but as I am using this to populate a drop-down where I need to have the name in the id column, i need it to be called ‘id’).

I should point out that it does output an id column, but it is always "id":0.

Could anyone shed any light on what I need to do to get this column aliased as ‘id’?

Thanks!

  • 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-23T13:17:49+00:00Added an answer on May 23, 2026 at 1:17 pm

    I’m not sure of how you can do this in a Rails query statement. Rails is going to try and take over the id column, casting the value returned by the database as id with the type of column that id is (presumably integer). That’s why your id column keeps getting set to 0, because "string".to_i #=> 0

    However, there is a way to do it, once you have the results back.

    Since you have the question tagged as Rails 3, it is preferable to use the new ActiveRelation syntax. You can do the following:

    # First, get the results from the query, then loop through all of them.
    Customer.select("last_name as 'ln', last_name as 'name'").all.collect do |c|
      # The first step of the loop is to get the attributes into a hash form
      h = c.attributes
      # The next step is to create an "id" key in the hash.
      # The Hash#delete method deletes the key/value pair at the key specified and returns the value.
      # We'll take that returned value and assign it to the just created "id" key.
      h["id"] = h.delete("ln")
      # And we have to call out the hash to ensure that it's the returned value from the collect.
      h
    end
    

    That will get you a hash with the id value as the text string value last_name and a name value as the same.

    Hope that helps!

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

Sidebar

Related Questions

Currently I have a loop that tries to find an unused filename by adding
Currently I have a bash script which runs the find command, like so: find
I currently have a query that looks like this: SELECT NON EMPTY ([Measures].[TOTAL]) ON
I have a one to many association in rails where a user has_many :albums
I currently have a list of documents, displayed one after another. When some of
I have 2 xibs, one for iPad and one for iPhone. However, currently I
I am currently writing a search method for my rails applications, and at the
Hey all, I have been away from rails for a while and have been
I have several threads running and I know currently one or many threads are
I have a method in a Silverlight app that currently returns an IList and

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.