Last active
January 2, 2016 17:49
-
-
Save gylaz/8339463 to your computer and use it in GitHub Desktop.
Polymorphic conditions
This file contains hidden or 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 Tag < ActiveRecord::Base | |
validates :name, presence: true, uniqueness: true | |
has_many :taggings | |
has_many :case_studies, through: :taggings, source: :taggable, source_type: 'CaseStudy' | |
has_many :posts, through: :taggings, source: :taggable, source_type: 'Post' | |
has_many :events, through: :taggings, source: :taggable, source_type: 'Event' | |
end |
This file contains hidden or 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 Tagging < ActiveRecord::Base | |
validates :tag, presence: true | |
validates :taggable_id, presence: true | |
validates :taggable_type, presence: true | |
belongs_to :tag | |
belongs_to :taggable, polymorphic: true | |
def self.published | |
joins_posts.joins_events.joins_case_studies. | |
where( | |
'posts.published = ? OR events.id IS NOT NULL OR case_studies.id IS NOT NULL', | |
true | |
) | |
end | |
def self.chronological | |
order('coalesce(posts.published_at, events.start_date, case_studies.created_at) DESC') | |
end | |
def self.joins_posts | |
joins <<-SQL | |
LEFT JOIN posts | |
ON posts.id = taggings.taggable_id | |
AND taggings.taggable_type = 'Post' | |
SQL | |
end | |
def self.joins_events | |
joins <<-SQL | |
LEFT JOIN events | |
ON events.id = taggings.taggable_id | |
AND taggings.taggable_type = 'Event' | |
SQL | |
end | |
def self.joins_case_studies | |
joins <<-SQL | |
LEFT JOIN case_studies | |
ON case_studies.id = taggings.taggable_id | |
AND taggings.taggable_type = 'CaseStudy' | |
SQL | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a genius solution. You are an amazing SQL engineer. I love you.