Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.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/8/python-3.x/18.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_Scope_Has Many_Belongs To - Fatal编程技术网

Ruby on rails 嵌套的作用域有许多关系

Ruby on rails 嵌套的作用域有许多关系,ruby-on-rails,scope,has-many,belongs-to,Ruby On Rails,Scope,Has Many,Belongs To,我有一个在Rails中的两个对象之间有很多关系,比如a,B和C class A < ActiveRecord::Base has_many :b end class B < ActiveRecord::Base belongs_to :a has_many :c end class C < ActiveRecord::Base belongs_to :b has_many :d end class D < ActiveRecord::Base

我有一个
在Rails中的两个对象之间有很多关系,比如
a
B
C

class A < ActiveRecord::Base
  has_many :b
end

class B < ActiveRecord::Base
  belongs_to :a
  has_many :c
end

class C < ActiveRecord::Base
  belongs_to :b
  has_many :d
end

class D < ActiveRecord::Base
  belongs_to :c
end

我应该如何做
范围:按a
?这是最好的方法吗?

我认为你有一个错误:应该是
scope:by_a->(aa){joins(:c)。where(c_id:Polygon.joins(:b)。where(b_id:b.joins(:a)。where(:a_id=>aa))
请更正此错误,将答案标记为正确。谢谢你的帮助!
scope :by_a ->(aa) {
  joins(:c).where(c_id: C.joins(:b).where(b_id: B.joins(:a).where(a_id: aa.id)))
}
scope :by_a ->(aa) {
  joins(:c).where(c_id: C.joins(:b).where(b_id: B.joins(:a).where(a_id: aa.id)))
}