title: Rails: get count of association through join table
body: <p>I have an entity called "<strong>Fact</strong>" this entity has a relationship with another entity called "<strong>User</strong>", this relationship is called "<strong>Feedback</strong>" that has 3 fields <strong>[user_id, fact_id, score]</strong> every time a person gives a feedback he gets who did the feedback (<strong>user_id</strong>) to what fact was made (<strong>fact_id</strong>) and what was qualified (<strong>0,1,2</strong>).
  Well, now I want to get the list of facts with their qualifications counted, example:</p>
  
  <pre><code>fact : {id: 3, name: "some name", number_of_zero: 7, number_of_one: 3, number_of_two: 3}
  </code></pre>
  
  <p>where number_of is the count of the times it was qualified.</p>
  
  <p><strong>Database Schema:</strong></p>
  
  <pre><code>  create_table "facts", force: :cascade do |t|
      t.string "title"
      t.string "description"
      t.integer "user_id"
    end
  
    create_table "users", force: :cascade do |t|
      t.string "name"
      t.string "last_name"
    end
  
  create_table "feedbacks", force: :cascade do |t|
      t.integer "score"
      t.integer "user_id"
      t.integer "fact_id"
      t.index ["fact_id"], name: "index_feedbacks_on_fact_id"
      t.index ["user_id"], name: "index_feedbacks_on_user_id"
    end
  </code></pre>
  
  <p><strong>Relations:</strong> </p>
  
  <pre><code>class Feedback &lt; ApplicationRecord
    belongs_to :user
    belongs_to :fact
  end
  </code></pre>