Hi, I am writing a small rails application that uses Ruby(1.8.5) with ActiveRecord (1.15.3) . I realized that if i use the find method with the optional parameter :select with only the required columns to look for records in a table with 50,000 records as opposed to a find method without the :select option, the query is a lot faster. It took me 31 secs in the former case as opposed to 12 in the latter. I am using MySQL and my application is running on windows. (And yes, the table that I am querying is indexed). Now my problem is that if i use :select the way it is mentioned in the rails documentation Item.find( :all, :select => [:column1, :column2, :column3] ) or Item.find( :all, :select => ['column1', 'column2', ':column3'] ) It throws an error because it executes a query like: select column1column2 column3 from items; when there is no column like column1column2column3 in the table items. However, If i modify my request and put a comma after each column name except on the last one Item.find( :all, :select => ['column1,' , 'column2,' , 'column3'] ) it works because this time it executes the query as noted below: select column1,column2,column3 from items; So I guess unless I am using some incompatible version, there could be a bug in ActiveRecord. Or am I missing something? Thanks
on 2007-04-15 00:11
on 2007-04-15 01:03
> Now my problem is that if i use :select the way it is mentioned in the > rails documentation > > Item.find( :all, :select => [:column1, :column2, :column3] ) or > Item.find( :all, :select => ['column1', 'column2', ':column3'] ) Where is that in the docs? :select should be a string. :select => "col1, col2, tbl2.*" -- Rick Olson http://lighthouseapp.com http://weblog.techno-weenie.net http://mephistoblog.com
on 2007-04-15 03:29
My apologies. I should have checked in the official rails docs. I was referring to http://www.rubyonrailsblog.com/articles/2006/10/04.... Thanks for clearing this up.