Not doing so has a few gotchas when working with relations, particularly with records which are not stored in the database. I'm excluding the related content file because it's got a very peculiar relationship with itself: the `has_one :opposite_related_content` has no inverse; the relation itself is its inverse. It's a false positive since the inverse condition is true: ``` content.opposite_related_content.opposite_related_content.object_id == content.object_id ```
19 lines
478 B
Ruby
19 lines
478 B
Ruby
module Relationable
|
|
extend ActiveSupport::Concern
|
|
|
|
included do
|
|
has_many :related_contents,
|
|
as: :parent_relationable,
|
|
inverse_of: :parent_relationable,
|
|
dependent: :destroy
|
|
end
|
|
|
|
def find_related_content(relationable)
|
|
RelatedContent.find_by(parent_relationable: self, child_relationable: relationable)
|
|
end
|
|
|
|
def relationed_contents
|
|
related_contents.not_hidden.map { |related_content| related_content.child_relationable }
|
|
end
|
|
end
|