首页 > 解决方案 > 制作一个类方法来统计评论

问题描述

我正在学习 Ruby on Rails,我想创建一个类方法来计算文章评论。我正在创建这个类方法

class_methods do
   def comment_count

   end
end

这是我的模式

create_table "articles", force: :cascade do |t|
    t.string "title"
    t.text "body"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "status", default: "public"
  end

  create_table "comments", force: :cascade do |t|
    t.string "commenter"
    t.text "body"
    t.integer "article_id", null: false
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "status"
    t.index ["article_id"], name: "index_comments_on_article_id"
  end

  add_foreign_key "comments", "articles"

标签: ruby-on-rails

解决方案


推荐阅读