Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rail4-如何创建多级关联?_Ruby On Rails_Ruby On Rails 4_Activerecord_Associations - Fatal编程技术网

Ruby on rails Rail4-如何创建多级关联?

Ruby on rails Rail4-如何创建多级关联?,ruby-on-rails,ruby-on-rails-4,activerecord,associations,Ruby On Rails,Ruby On Rails 4,Activerecord,Associations,我想得到以下值: 新建->类型->版本 我有以下设置: 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 classnew

我想得到以下值:

新建
->
类型
->
版本

我有以下设置:

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
classnew
如何访问
@new.type.version.first

谢谢

试试看:

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

@new.versions.first

因为有很多新的.versions.first
但是版本的形式是内部类型表单的一个字段,然后当我要用新表单创建注册表时,我选择类型,谁已经关联了版本。当我尝试@new.versions.first或@new.type.versions.first时,我得到:未定义的方法'version'用于#注意,
new
是ruby保留字,
type
是rails为STI保留字。你们的标题在这样的命名上遇到了麻烦…事实上,我并没有在项目中使用这个名称,只是举个例子