【问题标题】:Rails 4: association boolean in scopeRails 4:范围内的关联布尔值
【发布时间】:2015-03-18 23:31:12
【问题描述】:

我有两个模型之间的 has_and_belongs_to_many 关系,我想编写一个范围来检查关系上的布尔值是否为真:

class Component

  has_and_belongs_to_many :templates
  scope :editable, -> { where(template.showable) }

  ....
end

Template 模型上,:showable 是一个布尔值。我如何在 Rails 4 中编写这个?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 scope


    【解决方案1】:
    scope :editable, -> { joins(:templates).where( templates: { showable: true } ) }
    

    【讨论】:

    • 这很容易。干杯!
    猜你喜欢
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    相关资源
    最近更新 更多