https://rubygems.org/gems/postgres_ext implements this for rails up to 
version 4, and has ~2 million downloads.

https://github.com/kmurph73/ctes_in_my_pg ports the code to rails 5.

There are also numerous blog posts around documenting how to work around 
the issue by dropping down to raw Arel or string interpolation.

eg: 
 * 
http://www.scimedsolutions.com/articles/75-arel-part-ii-common-table-expressions
 * https://hashrocket.com/blog/posts/recursive-sql-in-activerecord
 * 
https://sonnym.github.io/2017/06/05/common-table-expressions-in-activerecord-a-case-study-of-quantiles/


https://www.cockroachlabs.com/blog/cockroachdb-hearts-activerecord-ruby-on-rails/
 
also mentions that ActiveRecord uses CTEs internally.

Would you agree that's a suitable level of usage to justify including this 
SQL-99 feature in core ActiveRecord? Or is the complexity likely to be too 
much for the number of users affected?

Thanks,

Daniel Heath

On Wednesday, January 16, 2019 at 11:21:42 AM UTC+11, Rafael Mendonça 
França wrote:
>
> Thank you for the feature proposal. It doesn't seem to be difficult to 
> implement this as a gem given most of the APIs you need in able to 
> implement something like this are public.
>
> What do you think about implementing this first as a gem and see if there 
> are adoption of this feature before proposing adding to the rails framework?
>
> Thanks.
>
> Rafael Mendonça França
> http://twitter.com/rafaelfranca
> https://github.com/rafaelfranca
>
>
> On Tue, Jan 15, 2019 at 7:12 PM Daniel Heath <daniel....@gmail.com 
> <javascript:>> wrote:
>
>> I'd like to be able to use my ActiveRecord scopes in a CTE for another 
>> scope. 
>>
>> Proposed syntax
>>
>>
>> class Post < ActiveRecord::Base
>> has_many :comments
>> end
>>
>> class Comment < ActiveRecord::Base
>> belongs_to :post
>> end
>>
>> Post.with(Comment.select(:post_id), name: :post_ids, recursive: 
>> false).where(id: {post_ids: :post_id})
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> Would a patch supporting this syntax be accepted?
>>
>> Thanks,
>>
>> Daniel Heath
>>
>> -- 
>> 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 <javascript:>.
>> To post to this group, send email to rubyonra...@googlegroups.com 
>> <javascript:>.
>> Visit this group at https://groups.google.com/group/rubyonrails-core.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
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-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to