Module: Torque::PostgreSQL::Relation::DistinctOn

Included in:
Torque::PostgreSQL::Relation
Defined in:
lib/torque/postgresql/relation/distinct_on.rb

Instance Method Summary collapse

Instance Method Details

#distinct_on(*value) ⇒ Object

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


29
30
31
# File 'lib/torque/postgresql/relation/distinct_on.rb', line 29

def distinct_on(*value)
  spawn.distinct_on!(*value)
end

#distinct_on!(*value) ⇒ Object

Like #distinct_on, but modifies relation in place.



34
35
36
37
# File 'lib/torque/postgresql/relation/distinct_on.rb', line 34

def distinct_on!(*value)
  self.distinct_on_values = value
  self
end

#distinct_on_valuesObject

:nodoc:



9
10
11
# File 'lib/torque/postgresql/relation/distinct_on.rb', line 9

def distinct_on_values
  @values.fetch(:distinct_on, FROZEN_EMPTY_ARRAY)
end

#distinct_on_values=(value) ⇒ Object

:nodoc:



13
14
15
16
# File 'lib/torque/postgresql/relation/distinct_on.rb', line 13

def distinct_on_values=(value)
  assert_modifiable!
  @values[:distinct_on] = value
end