ActiveRecord raw result method.

2,124 views
Skip to first unread message

Evgeniy Sokovikov

unread,
Apr 7, 2014, 4:05:25 AM4/7/14
to rubyonra...@googlegroups.com
Hello. Is it possible to get raw sql query result from ActiveRecord::Relation ?

Now I can make it this way:

sql = Note.select('count(*), commit_id').group(:commit_id).to_sql
ActiveRecord::Base.connection.execute(sql).to_a

I would like something like this:

Note.select('count(*), commit_id').group(:commit_id).raw

But didn't find such method.

Dheeraj Kumar

unread,
Apr 10, 2014, 12:03:48 PM4/10/14
to rubyonra...@googlegroups.com

+1, I do this a lot.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Rafael Mendonça França

unread,
Apr 10, 2014, 12:41:36 PM4/10/14
to rubyonra...@googlegroups.com
+1, too.

I was planning to add this method because calling `to_sql` is not a good solution. I think the name we choose was `select_all`.

Yves Senn

unread,
Apr 10, 2014, 12:48:20 PM4/10/14
to rubyonra...@googlegroups.com
+1 from me as well. This would be a great addition to the current API.

Cheers,
— Yves Senn

Xavier Noria

unread,
Apr 10, 2014, 12:52:14 PM4/10/14
to rubyonrails-core
Should that mimick the low-level API? select_value, select_rows, etc?

On the other hand I feel Arel and #to_sql are out of hand. In the beginning this was considered to be private to AR (this was certainly the point of view of Pratik, who implemented the initial integration IIRC). AR could change completely Arel with something else in theory as long as the public interface was respected.

But Arel and #to_sql seem to be used widely :(.

Dheeraj Kumar

unread,
Apr 10, 2014, 12:53:18 PM4/10/14
to rubyonra...@googlegroups.com
How about #to_a and #to_h instead?

#to_h would return the results as a hash, useful when you are retrieving a single row, but many columns.
#to_a would return the results as an array of hashes, useful when you have multiple rows.

Rafael Mendonça França

unread,
Apr 10, 2014, 12:57:20 PM4/10/14
to rubyonra...@googlegroups.com

to_a is already used to return an Array of objects.

select_rows seems good to me.

But Arel and #to_sql seem to be used widely.

Yes, this is sad. They was never public API. But I think we can’t do anything besides make clear what is public and what is private API.

Yves Senn

unread,
Apr 10, 2014, 1:03:56 PM4/10/14
to rubyonra...@googlegroups.com
We should keep #to_sql and Arel as private API. The use-case to get the raw response from the driver remains.
It would be nice to use ActiveRecord to build the SQL but get access to the raw driver result and not the mapped objects.

Do the lower level methods like `select_all`, `select_values`, etc. accept Relations?

  * The method is in the docs, so it’s assumed to be public.
  * We name the argument arel, which let’s you in belief that arel is public as well
  * The example uses raw SQL.

It would be good to get some light into the dark.

Dheeraj Kumar

unread,
Apr 10, 2014, 1:06:44 PM4/10/14
to rubyonra...@googlegroups.com
#to_sql + #execute is widely used, because it's a legitimate, and popular use case. Sometimes, we just need pure ruby objects. AR models become an overhead in those situations.

Rafael Mendonça França

unread,
Apr 10, 2014, 1:07:52 PM4/10/14
to rubyonra...@googlegroups.com
I was discussing with Aaron and we believe that passing relations to these methods should not be advised. This is why we want to add a method to relation to get the low level values.

Dheeraj Kumar

unread,
Apr 10, 2014, 1:09:43 PM4/10/14
to rubyonra...@googlegroups.com
so... `select_one` and `select_all`, maybe?

Xavier Noria

unread,
Apr 10, 2014, 2:32:16 PM4/10/14
to rubyonrails-core
On Thu, Apr 10, 2014 at 7:06 PM, Dheeraj Kumar <a.dheer...@gmail.com> wrote:

#to_sql + #execute is widely used, because it's a legitimate, and popular use case. Sometimes, we just need pure ruby objects. AR models become an overhead in those situations.

AR::Base#connection and its API are totally public, but #to_sql wasn't intended to be. AR is not a SQL builder, it is a layer between Ruby and databases.

That's why guides have never covered Arel etc., those are tools used by AR to build SQL *internally*, not to be exposed to end-users.

I'd bet #to_sql ended up in api.rubyonrails.org because nobody nodoc'ed it, I doubt it was intentional.

lingceng

unread,
Mar 14, 2016, 2:45:51 PM3/14/16
to Ruby on Rails: Core
I know it's been a long time from the last reply.
But I really want some method return raw results in ActiveRecord.

Actually I use ActiveRecord as a sql builder for some complex query. And it's useful in most cases.
I added a patch for my project to do the following query:

    PayRecord.group(:settlement_id, :pm).having('count(*) > 1').select("count(*), id, settlement_id, pm").select_all

Here's the patch
  
    class ActiveRecord::Relation
      def select_all
        @klass.connection.select_all(self.to_sql)
      end
    end

@Rafael Mendonça França What's the schedule of this feature?

Artemiy Solopov

unread,
Jul 10, 2018, 9:08:20 AM7/10/18
to Ruby on Rails: Core
I'd like a way to work with raw data as it was returned by a SQL query. Mostly because Rails' eager loading has a penchant for not working with ActiveRecord::QueryMethods.select, losing useful data in the process.

As for Arel and #to_sql... Why wasn't it made public? Consider that ActiveRecord doesn't even have a convenient interface for writing greater than/less than queries. Arel could be an answer to this problem, and more.

понедельник, 14 марта 2016 г., 21:45:51 UTC+3 пользователь lingceng написал:

Daniel Schepers

unread,
Sep 4, 2018, 10:13:50 PM9/4/18
to Ruby on Rails: Core
* bump * I think this would be a useful feature to have

Marco Costa

unread,
Sep 11, 2018, 1:53:22 AM9/11/18
to Ruby on Rails: Core
Going through the review process soon.
Reply all
Reply to author
Forward
0 new messages