与has_many建立联系:通过多个模型

时间:2011-06-22 11:21:55

标签: ruby-on-rails associations

请帮助了解如何@project.payments拥有此树:

Project
   |__Stages
         |__Costs
              |__Payments

project.rb

 has_many :stages
 has_many :costs, :through => stages

stage.rb

belongs_to :project
has_many :costs
has_many :payments :through => costs

cost.rb

belongs_to :stage
has_many :payments

payment.rb

belongs_to :cost

1 个答案:

答案 0 :(得分:3)

注意:由于这是一个嵌套的has_many:through关系,它只适用于Rails 3.1+(3.1的RC4已经出局)

project.rb

has_many :payments, :through => costs