Skip to content

Instantly share code, notes, and snippets.

@trushkevich
Forked from mrbrdo/rails_group_count.rb
Last active August 29, 2015 14:02
Show Gist options
  • Save trushkevich/3faed6a1f96ed1b9837e to your computer and use it in GitHub Desktop.
Save trushkevich/3faed6a1f96ed1b9837e to your computer and use it in GitHub Desktop.

Revisions

  1. trushkevich renamed this gist Jun 3, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. trushkevich revised this gist Jun 3, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails_group_count.rb
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ module ExtensionMethods
    def count(*args)
    scope = except(:select).select("1")
    query = "SELECT count(*) AS count_all FROM (#{scope.to_sql}) x"
    ActiveRecord::Base.connection.execute(query).first.try(:[], "count_all").to_i
    ActiveRecord::Base.connection.execute(query).first.first
    end
    end

  3. @mrbrdo mrbrdo revised this gist May 30, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion rails_group_count.rb
    Original file line number Diff line number Diff line change
    @@ -19,4 +19,6 @@ def returns_count_sum
    ActiveRecord::Base.send :include, ActiveRecordGroupCount

    # usage:
    # Model.group(:something).returns_count_sum.count
    # Model.group(:something).returns_count_sum.count
    # faster Kaminari pagination:
    # Model.returns_count_sum.page(1).per(10).total_pages
  4. @mrbrdo mrbrdo revised this gist May 30, 2013. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions rails_group_count.rb
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,17 @@
    module ActiveRelationGroupCount
    def count(*args)
    scope = except(:select).select("1")
    query = "SELECT count(*) AS count_all FROM (#{scope.to_sql}) x"
    ActiveRecord::Base.connection.execute(query).first.try(:[], "count_all").to_i
    end
    end

    module ActiveRecordGroupCount
    extend ActiveSupport::Concern

    module ExtensionMethods
    def count(*args)
    scope = except(:select).select("1")
    query = "SELECT count(*) AS count_all FROM (#{scope.to_sql}) x"
    ActiveRecord::Base.connection.execute(query).first.try(:[], "count_all").to_i
    end
    end

    module ClassMethods
    def returns_count_sum
    scoped.extending(ActiveRelationGroupCount)
    scoped.extending(ExtensionMethods)
    end
    end
    end
  5. @mrbrdo mrbrdo renamed this gist May 30, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. @mrbrdo mrbrdo created this gist May 30, 2013.
    22 changes: 22 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    module ActiveRelationGroupCount
    def count(*args)
    scope = except(:select).select("1")
    query = "SELECT count(*) AS count_all FROM (#{scope.to_sql}) x"
    ActiveRecord::Base.connection.execute(query).first.try(:[], "count_all").to_i
    end
    end

    module ActiveRecordGroupCount
    extend ActiveSupport::Concern

    module ClassMethods
    def returns_count_sum
    scoped.extending(ActiveRelationGroupCount)
    end
    end
    end

    ActiveRecord::Base.send :include, ActiveRecordGroupCount

    # usage:
    # Model.group(:something).returns_count_sum.count