【问题标题】:Rails nested has_many with additional contextRails 嵌套 has_many 和附加上下文
【发布时间】:2019-02-12 04:07:01
【问题描述】:

我试图描述一组软件服务共享相似依赖项但并非所有服务都在给定依赖项的相同版本上的想法。

考虑以下模型:

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 表中,但感觉是错误的解决方案。

谢谢

【问题讨论】:

    标签: ruby-on-rails activerecord has-many-through


    【解决方案1】:

    我想我会添加类似的东西

    class Configuration < AR
      belongs_to :service
      belongs_to :dependency
      belongs_to :dependency_version
    end
    

    是的,您会重复信息,但看起来这很有意义。可能性的实例化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      相关资源
      最近更新 更多