How to join a table and count records in Rails 3?

前端 未结 2 453
难免孤独
难免孤独 2021-02-07 10:13

I have a Collection class which has many coins. I am trying to select collections which have more than two coins. Currently, I have no problem doing that through st

相关标签:
2条回答
  • 2021-02-07 10:47

    To answer my own question:

    Collection.joins(:coins).group("coins.collection_id").having("count(coins.id) > 2")
    

    Hat tip to KJF who asked this similar question and to krakover for answering it.

    0 讨论(0)
  • 2021-02-07 11:10

    Add counter_cache columns and query them.

    http://railscasts.com/episodes/23-counter-cache-column

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