【问题标题】:Rails - Multi-level relationship modelsRails - 多级关系模型
【发布时间】:2016-02-24 11:33:52
【问题描述】:

所以,我的模型如下:

模型1 > 模型2 > 模型3 > 模型4 > 模型5

model1.rb

has_many :model2, dependent: true
has_many :model3, through: :model2
has_many :model4, ...?
has_many :model5, ...?

如何告诉 Rails Model1 有很多 Model4 和 Model5?

另外,对于model4,我这样做是不是太过分了:

add_foreign_key :model5, [:model1, :model2, :model3, :model4]

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    你应该可以这样做:

    has_many :model3, through: :model2
    has_many :model4, through: :model3
    has_many :model5, through: :model4
    

    尝试一下,如果有效,它就有效。如果没有,请尝试其他方法。

    【讨论】:

      【解决方案2】:

      你应该能够告诉 rails 你的 model1 有很多 model4 和 5。

      class Model1 < ActiveRecord::Base
      has_many :model4s
      has_many :model5s
      end
      

      在各自的模型 4 和 5 中

      belongs_to :model1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-15
        相关资源
        最近更新 更多