【问题标题】:pass array to scope将数组传递给范围
【发布时间】:2018-06-26 16:43:23
【问题描述】:
scope :colored_product, -> (color) {joins(:properties).where("properties.description=?",color)}

Product.colored_product("red") 将查找属性描述为“红色”的所有产品。

我想查找 properties.description 为“红色”或“蓝色”的产品。即

Product.colored_product("red" or "blue)

【问题讨论】:

    标签: mysql ruby-on-rails activerecord scope


    【解决方案1】:

    这应该适合你。

    scope :colored_product, -> (color) {joins(:properties).where("properties.description in (?)", color)}
    Product.colored_product(["red", "blue])
    

    来源:http://guides.rubyonrails.org/active_record_querying.html#subset-conditions

    【讨论】:

      猜你喜欢
      • 2017-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-21
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      • 2015-02-08
      相关资源
      最近更新 更多