1
0
Fork 0

Use scopes

This commit is contained in:
Alex Kotov 2019-07-26 08:58:39 +05:00
parent 7ff73cf588
commit a354e1541d
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
2 changed files with 14 additions and 12 deletions

View file

@ -22,7 +22,7 @@ class RegionalOffice < ApplicationRecord
has_many :current_supporter_relationships,
lambda {
select('DISTINCT ON (relationships.person_id) *')
.where(status: :supporter)
.supporters
.order(person_id: :asc, from_date: :desc)
},
class_name: 'Relationship',
@ -31,7 +31,7 @@ class RegionalOffice < ApplicationRecord
has_many :current_member_relationships,
lambda {
select('DISTINCT ON (relationships.person_id) *')
.where(status: :member)
.members
.order(person_id: :asc, from_date: :desc)
},
class_name: 'Relationship',
@ -40,7 +40,7 @@ class RegionalOffice < ApplicationRecord
has_many :current_regional_manager_relationships,
lambda {
select('DISTINCT ON (relationships.person_id) *')
.where(status: :member, role: :regional_manager)
.regional_managers
.order(person_id: :asc, from_date: :desc)
},
class_name: 'Relationship',
@ -49,7 +49,7 @@ class RegionalOffice < ApplicationRecord
has_many :current_regional_supervisor_relationships,
lambda {
select('DISTINCT ON (relationships.person_id) *')
.where(status: :member, role: :regional_supervisor)
.regional_supervisors
.order(person_id: :asc, from_date: :desc)
},
class_name: 'Relationship',

View file

@ -32,19 +32,21 @@ class Relationship < ApplicationRecord
scope :members, -> { where(status: :member) }
scope :federal_managers, -> { where(role: :federal_manager) }
scope :federal_managers, -> { members.where(role: :federal_manager) }
scope :federal_supervisors, -> { where(role: :federal_supervisor) }
scope :federal_supervisors, -> { members.where(role: :federal_supervisor) }
scope :federal_secretaries,
-> { where(federal_secretary_flag: :federal_secretary) }
scope :federal_secretaries, lambda {
federal_managers.where(federal_secretary_flag: :federal_secretary)
}
scope :regional_managers, -> { where(role: :regional_manager) }
scope :regional_managers, -> { members.where(role: :regional_manager) }
scope :regional_supervisors, -> { where(role: :regional_supervisor) }
scope :regional_supervisors, -> { members.where(role: :regional_supervisor) }
scope :regional_secretaries,
-> { where(regional_secretary_flag: :regional_secretary) }
scope :regional_secretaries, lambda {
regional_managers.where(regional_secretary_flag: :regional_secretary)
}
###############
# Validations #