Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5863 (new enhancement)

Opened 2 years ago

Last modified 2 months ago

[PATCH] :select option for ActiveRecord#find should take an Array of fieldnames

Reported by: rubyonrails@beautifulpixel.com Assigned to: David
Priority: normal Milestone: 2.1
Component: ActiveRecord Version: edge
Severity: normal Keywords: tiny
Cc: bitsweat, tarmo

Description

Since most of the time what you pass to the :select option when using ActiveRecord#find is simply a list of column names, this patch allows you pass them in as an Array.

Before Patch:

Product.find(:first, :select => 'id, title')

After Patch:

Product.find(:first, :select => [:id, :title])

For one thing it keeps it entirely in ruby without going into SQL fragments if you don't need to. And it makes it easier to handle a dynamic value for the select option instead of one baked into the code.

This patch simply performs a "join(', ')" on the :select option if it responds_to?(:join). This stringifies the array, if necesary, before it gets inserted into the SQL statement.

NOTE: I have not verified the tests are valid because I have not been able to properly setup the ActiveRecord test environment. I can however verify that the patch works as expected in my development environment through the console, and through mongrel serving a page.

Attachments

select_option_for_find_should_accept_array.patch (1.4 kB) - added by rubyonrails@beautifulpixel.com on 08/23/06 01:37:47.
select_accepts_array.patch (1.6 kB) - added by veader on 03/05/08 15:56:49.

Change History

08/23/06 01:37:47 changed by rubyonrails@beautifulpixel.com

  • attachment select_option_for_find_should_accept_array.patch added.

08/23/06 05:23:29 changed by bitsweat

  • cc set to bitsweat.

Is this a common need? The :select option has always been 'bare metal' and it should be clear that tossing in a bunch of field names is not going to automagically qualify with table names and such.

08/07/07 06:05:18 changed by tarmo

  • cc changed from bitsweat to bitsweat, tarmo.

I agree with bitsweat here. There is little point in trying to make select look beautiful outside when on the inside it's still ugly, especially if this could lead someone to using select for the wrong reasons.

09/08/07 11:12:58 changed by alexpeattie

I disagree. I feel that it is counterintuitive to not offer this option. Tt feels both more "Railsish", and is more consistent with the formats accepted by options like :include.

03/05/08 15:56:49 changed by veader

  • attachment select_accepts_array.patch added.

03/05/08 16:01:49 changed by veader

  • milestone changed from 1.2.7 to 2.0.3.

This gets a +1 for me (and I've included an updated patch from edge).

I agree with the original author that this is something that should be included.

It's common enough that I created my version of the patch before I found this ticket. In our application we have several tables that contain a lot of data and we use the :select in finders very frequently to avoid pulling back unneeded data.

This feels and looks natural with other patterns found in find, like the hash for :conditions. You can already use :select => :column so :select => [:column1, :column2] doesn't seem to be a stretch.

03/05/08 17:42:46 changed by randy

  • cc changed from bitsweat, tarmo to bitsweat, tarmo, veader.
  • owner changed from David to randy.
  • status changed from new to assigned.

I like it. An array syntax to provide a list of columns seems so natural that many people would be surprised to find it doesn't work. And I don't want to discourage anybody from reducing the pointless bloat of a large query!

03/05/08 17:44:31 changed by randy

  • cc deleted.
  • owner changed from randy to David.
  • status changed from assigned to new.

03/05/08 23:42:30 changed by bitsweat

  • cc set to bitsweat, tarmo.
  • milestone changed from 2.0.3 to 2.1.