Describes a relationship between two entities. A relationship is detected based on Active Record associations. One relationship may represent more than one association, however. Related associations are grouped together. Associations are related if they share the same foreign key, or the same join table in the case of many-to-many associations.
Methods
public instance
Constants
| N | = | Cardinality::N |
Attributes
| destination | [R] | The destination entity. It corresponds to the model that has defined a belongs_to association with the other model. |
| domain | [R] | The domain in which this relationship is defined. |
| source | [R] | The source entity. It corresponds to the model that has defined a has_one or has_many association with the other model. |
Public instance methods
Returns all Active Record association objects that describe this relationship.
# File lib/rails_erd/domain/relationship.rb, line 73 def associations @forward_associations + @reverse_associations end
Returns the cardinality of this relationship.
# File lib/rails_erd/domain/relationship.rb, line 78 def cardinality @cardinality ||= begin reverse_max = any_habtm?(associations) ? N : 1 forward_range = associations_range(@forward_associations, N) reverse_range = associations_range(@reverse_associations, reverse_max) Cardinality.new(reverse_range, forward_range) end end
Indicates if a relationship is indirect, that is, if it is defined through other relationships. Indirect relationships are created in Rails with has_many :through or has_one :through association macros.
# File lib/rails_erd/domain/relationship.rb, line 91 def indirect? !@forward_associations.empty? and @forward_associations.all?(&:through_reflection) end
Indicates whether the source cardinality class of this relationship is equal to infinity. This is true for many-to-many relationships only.
# File lib/rails_erd/domain/relationship.rb, line 129 def many_to? cardinality.cardinality_class[0] != 1 end
Indicates whether or not the relationship is defined by two inverse associations (e.g. a has_many and a corresponding belongs_to association).
# File lib/rails_erd/domain/relationship.rb, line 98 def mutual? @forward_associations.any? and @reverse_associations.any? end
Indicates whether the source cardinality class of this relationship is equal to one. This is true for one-to-one or one-to-many relationships only.
# File lib/rails_erd/domain/relationship.rb, line 123 def one_to? cardinality.cardinality_class[0] == 1 end
Indicates whether or not this relationship connects an entity with itself.
# File lib/rails_erd/domain/relationship.rb, line 103 def recursive? @source == @destination end
The strength of a relationship is equal to the number of associations that describe it.
# File lib/rails_erd/domain/relationship.rb, line 135 def strength if source.generalized? then 1 else associations.size end end
Indicates whether the destination cardinality class of this relationship is equal to infinity. This is true for one-to-many or many-to-many relationships only.
# File lib/rails_erd/domain/relationship.rb, line 116 def to_many? cardinality.cardinality_class[1] != 1 end
Indicates whether the destination cardinality class of this relationship is equal to one. This is true for one-to-one relationships only.
# File lib/rails_erd/domain/relationship.rb, line 109 def to_one? cardinality.cardinality_class[1] == 1 end