Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.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 我如何通过一个特定的ID通过一个作用域编写一个has\u many?_Ruby On Rails_Postgresql_Activerecord_Scope_Has And Belongs To Many - Fatal编程技术网

Ruby on rails 我如何通过一个特定的ID通过一个作用域编写一个has\u many?

Ruby on rails 我如何通过一个特定的ID通过一个作用域编写一个has\u many?,ruby-on-rails,postgresql,activerecord,scope,has-and-belongs-to-many,Ruby On Rails,Postgresql,Activerecord,Scope,Has And Belongs To Many,我的模型是这样写的: class Registration < ActiveRecord::Base has_many :registered_courses has_many :courses, through: :registered_courses has_many :sections, through: :courses 它返回来自同一课程的任何注册的部分 但我真正想要的是,所有的部分都只来自那个特定的注册。也就是说,即使其他注册使用相同的课程。除非您的部分直接属

我的模型是这样写的:

class Registration < ActiveRecord::Base

  has_many :registered_courses
  has_many :courses,  through: :registered_courses
  has_many :sections, through: :courses
它返回来自同一课程的任何注册的部分


但我真正想要的是,所有的部分都只来自那个特定的注册。也就是说,即使其他注册使用相同的课程。

除非您的部分直接属于注册,否则您无法获得属于特定注册的部分。

啊,好的,这很有意义。那么有没有其他方法可以实现这一点?@Trip您可以在注册和分区之间添加常用的has_many-behing_to,并在触发任何事件(例如课程创建或其他)时创建关系,而不是您想要的。当您通过关联获取多个信息时,rails的工作方式只有一种:1。通过关联模型发现,2。然后获得目标关联。我有点理解你的解释,但无法将代码部分可视化。。我会继续胡闹。谢谢
Registration.find(x).sections