Sha256: a89d284458652a3fdc795cd0295b3f7008803124ff2ea46d4a82afaabcf54486

Contents?: true

Size: 1.19 KB

Versions: 12

Compression:

Stored size: 1.19 KB

Contents

module Torque
  module PostgreSQL
    module Relation
      module DistinctOn

        # :nodoc:
        def distinct_on_values; get_value(:distinct_on); end
        # :nodoc:
        def distinct_on_values=(value); set_value(:distinct_on, value); end

        # 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_values = value
          self
        end

        private

          # Hook arel build to add the distinct on clause
          def build_arel
            arel = super
            value = self.distinct_on_values
            arel.distinct_on(resolve_column(value)) if value.present?
            arel
          end

      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
torque-postgresql-0.2.12 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.2.11 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.2.10 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.2.9 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.2.8 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.2.7 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.2.6 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.2.5 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.2.4 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.2.3 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.2.2 lib/torque/postgresql/relation/distinct_on.rb
torque-postgresql-0.2.1 lib/torque/postgresql/relation/distinct_on.rb