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

前端 未结 3 624
情书的邮戳
情书的邮戳 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: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
    

提交回复
热议问题