【问题标题】:scope filter records depends on associations count范围过滤记录取决于关联计数
【发布时间】:2014-12-30 05:59:15
【问题描述】:

我使用了 MongoDB,我有模型 retailer 有很多 stores,我想退货至少有一家商店的零售商。

我发现了这个问题:How can I find records by “count” of association using rails and mongoid?

但公认的答案是:

scope :with_at_least_n_stores, -> { where('stores_count >= 1') }

取决于零售商模型中的stores_count 字段,但我没有此字段。

任何解决方案都可以找到至少拥有一家使用范围而不添加字段为stores_count 的商店的零售商?

【问题讨论】:

    标签: ruby-on-rails mongodb scope has-many


    【解决方案1】:

    也许是这个?

    scope :with_at_least_n_stores, -> { stores.size > 0 }
    

    【讨论】:

    • 不,我之前试过,得到了这个错误NameError: undefined local variable or method 'stores' for Retailer:Class
    • 好的,在这里查看 Patrick 的回答:stackoverflow.com/questions/8223518/…
    猜你喜欢
    • 1970-01-01
    • 2012-02-26
    • 2022-12-18
    • 2022-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多