How can I find records by “count” of association using rails and mongoid?

前端 未结 3 625
情书的邮戳
情书的邮戳 2021-02-04 16:43

With these models:

class Week
  has_many :proofs
end
class Proof
  belongs_to :week
end

I want to do something like:

Week.where         


        
相关标签:
3条回答
  • 2021-02-04 17:11

    With rails (and without counter_cache), you could do:

    class Week < ActiveRecord::Base
      has_many :proofs
    
      def self.by_proofs_size
        sort_by { |week| week.proofs.size }
      end
    
      def self.with_at_least_n_proofs(n = 1)
        select { |week| week.proofs.size >= n }
      end
    end
    

    Even though each of those operations produces 2 queries, this is far from ideal.

    The pair of queries is repeated (=> 4 queries for each operation) with scopes (bug?):

    scope :with_at_least_n_proofs, -> (n = 1) { select { |w| w.proofs.size >= n } }
    scope :by_proofs_size, -> { sort_by { |w| w.proofs.size } }
    

    The ideal is probably to use counter_cache

    scope :with_at_least_n_proofs, -> (n = 1) { where('proofs_count >= ?', n) }
    scope :by_proofs_size, -> { order(proofs_count: :desc) }
    
    0 讨论(0)
  • 2021-02-04 17:23

    I don't know if this is the best solution, as it maps it through a array, but this does the job: (the other solutions mentioned here gives me exceptions)

    class Week < ActiveRecord::Base
    
      scope :has_proofs, -> { any_in(:_id => includes(:proofs).select{ |w| w.proofs.size > 0 }.map{ |r| r.id }) }
    
    end
    
    0 讨论(0)
  • 2021-02-04 17:24

    Pardon me if I'm way off - but would you be able to use a simple counter_cache in the weeks table? Then you could do something like week.proofs_count.

    0 讨论(0)
提交回复
热议问题