Sha256: 62be710627f1b508f82144fa65ec2c8cb3ad42cdffd7c32b263475db9bca8841

Contents?: true

Size: 1.06 KB

Versions: 8

Compression:

Stored size: 1.06 KB

Contents

module Torque
  module PostgreSQL
    module Relation
      module DistinctOn

        attr_accessor :distinct_on_value

        # Specifies whether the records should be unique or not by a given set of fields.
        # For example:
        #
        #   User.distinct_on(:name)
        #   # Returns 1 record per distinct name
        #
        #   User.distinct_on(:name, :email)
        #   # Returns 1 record per distinct name and email
        #
        #   User.distinct_on(false)
        #   # You can also remove the uniqueness
        def distinct_on(*value)
          spawn.distinct_on!(*value)
        end

        # Like #distinct_on, but modifies relation in place.
        def distinct_on!(*value)
          self.distinct_on_value = value
          self
        end

        private

          # Hook arel build to add the distinct on clause
          def build_arel
            arel = super

            value = self.distinct_on_value
            arel.distinct_on(resolve_column(value)) unless value.nil?
            arel
          end

      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
torque-postgresql-0.1.7 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.1.6 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.1.5 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.1.4 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.1.3 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.1.2 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.1.1 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.1.0 lib/torque/postgresql/relation/distinct_on.rb