Commit b61c3320 by thanhnd

scope to class method

parent cc8a60f1
Pipeline #548 failed with stages
in 0 seconds
...@@ -2,9 +2,21 @@ class City < ApplicationRecord ...@@ -2,9 +2,21 @@ class City < ApplicationRecord
belongs_to :area belongs_to :area
has_many :jobs has_many :jobs
validates_presence_of :city_name validates_presence_of :city_name
scope :top_city_by_job, ->{joins(:jobs).select('cities.*, COUNT(jobs.id) as job_count').group('jobs.city_id').order(:job_count).reverse_order.first(9)}
scope :top_city_by_job_nn, ->{joins(:jobs).where("cities.area_id = 2").select('cities.*, COUNT(jobs.id) as job_count').group('jobs.city_id').order(:job_count).reverse_order.first(9)} def self.top_city_by_job
City.joins(:jobs).select('cities.*, COUNT(jobs.id) as job_count').group('jobs.city_id').order(:job_count).reverse_order.first(9)
scope :all_city_by_job, ->{joins(:jobs).select('cities.*, COUNT(jobs.id) as job_count').group('jobs.city_id').order(:job_count).reverse_order} end
scope :all_city_by_job_nn, ->{joins(:jobs).where("cities.area_id = 2").select('cities.*, COUNT(jobs.id) as job_count').group('jobs.city_id').order(:job_count).reverse_order}
def self.top_city_by_job_nn
City.joins(:jobs).where("cities.area_id = 2").select('cities.*, COUNT(jobs.id) as job_count').group('jobs.city_id').order(:job_count).reverse_order.first(9)
end
def self.all_city_by_job
City.joins(:jobs).select('cities.*, COUNT(jobs.id) as job_count').group('jobs.city_id').order(:job_count).reverse_order
end
def self.all_city_by_job_nn
City.joins(:jobs).where("cities.area_id = 2").select('cities.*, COUNT(jobs.id) as job_count').group('jobs.city_id').order(:job_count).reverse_order
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment