我在两个模型之间有一个has_and_belongs_to_many关系,我想写一个范围来检查该关系上的布尔值是否为真:
class Component
has_and_belongs_to_many :templates
scope :editable, -> { where(template.showable) }
....
end
在Template
模型上,:showable
是一个布尔值。我如何在Rails4中写这篇文章?
scope :editable, -> { joins(:templates).where( templates: { showable: true } ) }