Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

.select and :select in AR does not work correctly with an array

#4562

ActiveRecord support for using an array of symbols to specify columns to include in a find no longer works. Instead AR/Arel only uses the last value in the array for the select/projection.

Failing test case is provided.

Reported by Josh Kalderimis · May 9th, 2010 @ 01:12 PM

State: stale
Milestone: 3.1
Assigned to: Carl Lerche Carl Lerche
Importance: none

Activity

  1. Josh Kalderimis
  2. Dan Pickett
    Dan Pickett
    • Tag changed from activerecord, arel, rails3 to activerecord, arel, bugmash, rails3

    May 9th, 2010 @ 06:38 PM

  3. Neeraj Singh
    Neeraj Singh

    Attached is a patch against rails3 edge.

    @Josh thanks for the failing test.

    Current code is given below.

    # query_methods.rb
    selects = @select_values.uniq
    if selects.present?
      selects.each do |s|
        @implicit_readonly = false
        arel = arel.project(s) if s.present?
      end
    else
      arel = arel.project(quoted_table_name + '.*')
    end
    

    As you can see for each select column arel.project(s) is being called. However multiple project is specified on arel then the last one wins.

    Take a look at this test.

    users = Table(:users)
    users.project(users[:email]).project(users[:phone]).to_sql
    "SELECT     \"users\".\"phone\" FROM       \"users\""
    

    The correct way to handle this would be.

    users.project(users[:email],users[:phone]).to_sql
    "SELECT     \"users\".\"email\", \"users\".\"phone\" FROM       \"users\""
    

    Attached patch provides the all the columns names to get selected columns.

    May 11th, 2010 @ 06:09 PM

  4. Josh Kalderimis
    Josh Kalderimis

    Hi Neeraj Singh,

    Can you update the ticket as verified. Also, have the run all the AR tests?

    Thanks

    Josh

    May 11th, 2010 @ 07:58 PM

  5. Neeraj Singh
    Neeraj Singh
    • Tag changed from activerecord, arel, bugmash, rails3 to activerecord, arel, bugmash, patch, rails3

    yes all the tests are passing. I have updated the tag to 'patched'.

    May 11th, 2010 @ 08:19 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • Milestone cleared.
    • State changed from new to verified

    Proper patch here, also have the tests made by Josh Kalderimis and maintained as the committer.

    May 14th, 2010 @ 06:52 PM

  7. Neeraj Singh
    Neeraj Singh

    @Santiago Thanks for the much better fix. Now I know better. :-)

    May 14th, 2010 @ 07:44 PM

  8. Santiago Pastorino
    Santiago Pastorino
    • Milestone set to 3.1

    For now you can use :select => "id, name" we need to uniformize the API and allow all methods accept and Array

    May 14th, 2010 @ 11:02 PM

  9. José Valim
    José Valim
    • Tag changed from activerecord, arel, bugmash, patch, rails3 to activerecord, arel, patch, rails3

    May 15th, 2010 @ 11:39 AM

  10. Rizwan Reza
    Rizwan Reza
    • Tag changed from activerecord, arel, patch, rails3 to activerecord, arel, bugmash-review, patch, rails3

    May 16th, 2010 @ 03:05 AM

  11. José Valim
    José Valim
    • Tag changed from activerecord, arel, bugmash-review, patch, rails3 to activerecord, arel, patch, rails3
    • State changed from verified to open

    We discussed this one earlier and decided to postpone to 3.1 since it has other implications in the source code.

    May 16th, 2010 @ 09:49 AM

  12. rails
    rails

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    May 4th, 2011 @ 01:00 AM

  13. rails
    rails
    • State changed from open to stale

    May 4th, 2011 @ 01:00 AM