Created
July 8, 2017 17:43
-
-
Save esparkman/df15e553d218c7ac85030e37b191c969 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Organization < ApplicationRecord | |
validates :name, presence: true, uniqueness: true | |
validates :description, presence: true | |
has_many :organization_categories | |
has_many :categories, through: :organization_categories | |
has_many :locations | |
def self.categories_query(category_ids) | |
if category_ids.count == 1 | |
joins(:organization_categories).where('organization_categories.category_id': category_ids.first) | |
else | |
joins(:organization_categories).where('organization_categories.organization_id': OrganizationCategory.select('organization_categories.organzation_id') | |
.where(category_id: category_ids) | |
.group('organization_categories.organization_id') | |
.having("COUNT(organization_categories.organization_id) > #{category_ids.count - 1}")) | |
.uniq | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment