Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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 一个模型中有多个层次_Ruby On Rails_Ruby_Activerecord_Has Many Through - Fatal编程技术网

Ruby on rails 一个模型中有多个层次

Ruby on rails 一个模型中有多个层次,ruby-on-rails,ruby,activerecord,has-many-through,Ruby On Rails,Ruby,Activerecord,Has Many Through,我正在使用RubyonRails(特别是ActiveRecord),我正在尝试决定使用多个级别链接我的模型是否是一个好主意 class Student < ActiveRecord::Base has_many :student_sections has_many :sections, :through => :student_sections has_many :courses, :through => :sections end class-Student:学

我正在使用RubyonRails(特别是ActiveRecord),我正在尝试决定使用多个级别链接我的模型是否是一个好主意

class Student < ActiveRecord::Base
  has_many :student_sections
  has_many :sections, :through => :student_sections
  has_many :courses, :through => :sections
end
class-Student:学生节
有很多:课程,:至=>:节
结束

这似乎是可行的,但我在ActiveRecord方面没有太多经验。有什么理由不这样做吗?

您需要添加:source属性

has_many :sections, :through => :student_sections, :source => 'your_source'

这很好,但您应该记住,课程关联实际上只是一个“get”关联(而不是“get and set”)。我的意思是你可以说

@学生课程

(在完成neo的修复后)获取课程列表,但您不能这样做


@student.courses他为什么需要添加源?在这种情况下,Rails应该能够自动确定关联。rdoc writed:“指定has_many:through查询使用的源关联名称。仅当无法从关联中推断名称时才使用它。拥有多个:订阅者,:至=>:订阅将在订阅中查找:订阅者或:订阅者,除非提供了:source。“我的模型设置为不需要使用:source,但谢谢。