Rails 嵌套 has_many 具有额外的上下文
Rails nested has_many with additional context
我试图描述这样的想法,即一组软件服务共享相似的依赖关系,但并非所有服务都在给定依赖关系的同一版本上。
考虑以下模型:
class Service < ApplicationRecord
has_many :service_dependencies
has_many :dependencies, through: :service_dependencies
end
class ServiceDependency < ApplicationRecord
belongs_to :service
belongs_to :dependency
end
class Dependency < ApplicationRecord
has_many :service_dependencies
has_many :services, through: :service_dependencies
has_many :versions, foreign_key: 'dependency_id', class_name: 'DependencyVersion'
end
class DependencyVersion < ApplicationRecord
belongs_to :dependency
end
虽然一个 service
可以有很多 dependencies
并且给定的依赖项可以有很多 versions
任何服务一次使用一个依赖项的版本。
如何表达这种关系,以便确定服务当前使用的依赖项版本?
我认为我可以将当前版本存储在 service_dependencies
table 上,但感觉这是错误的解决方案。
谢谢
我想我会添加类似
的内容
class Configuration < AR
belongs_to :service
belongs_to :dependency
belongs_to :dependency_version
end
是的,您会重复信息,但看起来这很有意义。可能性的实例化。
我试图描述这样的想法,即一组软件服务共享相似的依赖关系,但并非所有服务都在给定依赖关系的同一版本上。
考虑以下模型:
class Service < ApplicationRecord
has_many :service_dependencies
has_many :dependencies, through: :service_dependencies
end
class ServiceDependency < ApplicationRecord
belongs_to :service
belongs_to :dependency
end
class Dependency < ApplicationRecord
has_many :service_dependencies
has_many :services, through: :service_dependencies
has_many :versions, foreign_key: 'dependency_id', class_name: 'DependencyVersion'
end
class DependencyVersion < ApplicationRecord
belongs_to :dependency
end
虽然一个 service
可以有很多 dependencies
并且给定的依赖项可以有很多 versions
任何服务一次使用一个依赖项的版本。
如何表达这种关系,以便确定服务当前使用的依赖项版本?
我认为我可以将当前版本存储在 service_dependencies
table 上,但感觉这是错误的解决方案。
谢谢
我想我会添加类似
的内容class Configuration < AR
belongs_to :service
belongs_to :dependency
belongs_to :dependency_version
end
是的,您会重复信息,但看起来这很有意义。可能性的实例化。