# Schema: entries[ id, account_id, creator_id, created_at, updated_at, specialization_type, specialization_id ] class Entry < ApplicationRecord has_specialization types: %w[ Message Comment ] belongs_to :account belongs_to :creator end # Schema: messages[ id, subject ] class Message < ApplicationRecord is_specialization_of 'Entry' end # Schema: comments[ id, content ] class Comment < ApplicationRecord is_specialization_of 'Entry' end # Entry#specialization_class => +Message+ or +Comment+ # Entry#specialization_name => "message" or "comment" # Entry.specialization => message record, or comment record # Entry.messages => Entry.where(specialization_type: "Message") # Entry#message? => true when specialization_type == "Message" # Entry#message => returns the message record, when specialization_type == "Message", otherwise nil # Entry#message_id => returns specialization_id, when entryable_type == "Message", otherwise nil # Entry.comments => Entry.where(specialization_type: "Comment") # Entry#comment? => true when specialization_type == "Comment" # Entry#comment => returns the comment record, when specialization_type == "Comment", otherwise nil # Entry#comment_id => returns entryable_id, when specialization_type == "Comment", otherwise nil ################################################################################ # Association is named "specialization" by default, because 99% of the time you # only ever want one, but can be named manually to avoid conflicts if needed class Entry < ApplicationRecord has_specialization :entryable, types: %w[ Message Comment ] has_specialization :variant, types: %w[ Mobile Desktop ] end # Entry#entryable_class => +Message+ or +Comment+ # Entry#entryable_name => "message" or "comment" # Entry#entryable => message record, or comment record # # Entry#variant_class => +Mobile+ or +Desktop+ # Entry#variant_name => "mobile" or "desktop" # Entry#variant => mobile record, or desktop record