是否可以連接兩個表並計算兩者的關係數?理想的輸出是 [name, recommendation_count, review_count]
Rails has_many多次連接記錄數
class PatientProfile
has_many :reviews
has_many :recommendations
# this works
PatientProfile.joins(:reviews).group(:id).pluck(:name, 'count(reviews.id)')
# this does not work
PatientProfile.joins(:recommendations, :reviews).group(:id).pluck(:name, 'count(recommendations.id)', 'count(reviews.id)')
第二個例子爲列
你能提供有關您的模型/關係的細節? – archana
@archana增加了更多關於模型的信息 – user2954587
你的'has_many:through'關係在哪裏? – archana