Rail4 - 如何进行多级关联?

时间:2015-11-25 16:09:22

标签: ruby-on-rails ruby-on-rails-4 activerecord associations

我想获取值:

New - > Type - > Version

我有这个设置:

class New < ActiveRecord::Base
   has_one :type
end

class Type < ActiveRecord::Base
   belongs_to :new
   has_many :versions
end

class Version < ActiveRecord::Base
   belongs_to :type
end

如何访问@new.type.version.first

谢谢!

1 个答案:

答案 0 :(得分:2)

尝试:

class New < ActiveRecord::Base
  has_many :versions, through: :type
  ...
end

@ new.versions.first