mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-09 23:06:06 +00:00
13 lines
660 B
Ruby
13 lines
660 B
Ruby
class EnterpriseRelationship < ActiveRecord::Base
|
|
belongs_to :parent, class_name: 'Enterprise'
|
|
belongs_to :child, class_name: 'Enterprise'
|
|
|
|
validates_presence_of :parent_id, :child_id
|
|
validates_uniqueness_of :child_id, scope: :parent_id, message: "^That relationship is already established."
|
|
|
|
scope :with_enterprises,
|
|
joins('LEFT JOIN enterprises AS parent_enterprises ON parent_enterprises.id = enterprise_relationships.parent_id').
|
|
joins('LEFT JOIN enterprises AS child_enterprises ON child_enterprises.id = enterprise_relationships.child_id')
|
|
scope :by_name, with_enterprises.order('parent_enterprises.name, child_enterprises.name')
|
|
end
|