我目前有以下三种型号:
class Vacancy < ActiveRecord::Base
# Database Associations
has_many :vacancy_occupations, :foreign_key => :vacancy_id
has_many :occupations, :through => :vacancy_occupations
belongs_to :country
belongs_to :employer
# Database Validations
validates_presence_of :name, :title, :description
end
class VacancyOccupation < ActiveRecord::Base
belongs_to :vacancy, :foreign_key => :vacancy_id
belongs_to :occupation, :foreign_key => :concept_id
end
class Occupation < Concept
end
class Concept < ActiveRecord::Base
# Database Associations
has_many :labels
has_many :vacancy_occupations, :foreign_key => :concept_id
has_many :cv_occupations, :foreign_key => :concept_id
has_many :vacancies, :through => :vacancy_occupations
has_many :cvs, :through => :cv_occupations
# Database Validations
validates_presence_of :uri
validates_uniqueness_of :uri
end
我正在寻找一种机制来选择具有特定职业的所有职位空缺。目前我可以通过以下声明得到它:
@vacancies = Vacancy.joins(:vacancy_occupations).where('vacancy_occupations.concept_id' => occupation_ids).uniq
但我想知道是否有更清晰的语法来完成这项工作?一直在玩包含选项,尝试从不同的模型作为起点,但我似乎失去了绝望。
答案 0 :(得分:0)
我不确定这是否是正确的语法,但如果你能得到所有职业的父母,你基本上会得到所有职业空缺的清单。
Occupation.all.vacancies
答案 1 :(得分:0)
您可以使用范围来获得更清晰的语法