I currently have the following three models:
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
I’m looking for a mechanism that selects all vacancies that have a given occupation. Currently I can get it dony by the following statement:
@vacancies = Vacancy.joins(:vacancy_occupations).where('vacancy_occupations.concept_id' => occupation_ids).uniq
But I was wondering if there is a cleaner syntax to get this done? Been playing with the includes options and trying from different models as starting point but I seem to get hopelessly lost.
you may use scopes to get cleaner syntax