cyberfly
12/5/2019 - 3:11 AM

Rails scope from relations

# filter from has many relations where category 

scope :visitable_category, -> visitable_category { joins(:visit_plan_categories).where("visit_plan_categories.category = ?", visitable_category) }

scope :inspect_from, -> inspect_from { where("inspect_from >= ?", inspect_from) }

scope :inspect_to, -> inspect_to { where("inspect_to <= ?", inspect_to) }

scope :status, -> status { where(status: status) }

scope :approval, -> {
    pending_approval_statuses = %w(LEVEL_1_APPROVAL LEVEL_2_APPROVAL LEVEL_1_APPROVED LEVEL_2_APPROVED REJECTED)
    where(status: pending_approval_statuses)
}