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)))
}