У меня есть базовая ситуация blog
/ comments
в моем приложении, и я пытаюсь создать раздел "наиболее комментируемых блогов" в моем приложении.
У меня есть это для моей comment.rb
модели:
class Comment < ApplicationRecord
belongs_to :user, optional: true
belongs_to :blog, optional: true, counter_cache: true
end
И это в моем blogs_controller.rb
:
@commented_blogs = Blog.where("published_on <= ?", Date.today).order('comments_count DESC').limit(3)
Тем не менее, когда я пытаюсь перебрать @commented_blogs
в представлении, я получаю следующую ошибку:
SQLite3::SQLException: no such column: comments_count: SELECT "blogs".* FROM "blogs" WHERE (published_on <= '2018-12-06') ORDER BY comments_count DESC LIMIT ?
Мне кажется, что мне нужно добавить столбец в базу данных, но в документации, на которую я смотрел, не упоминалось никаких миграций.Кто-нибудь может помочь?
ДОПОЛНИТЕЛЬНАЯ ИНФОРМАЦИЯ
Вот моя схема для blogs
и comments
:
create_table "blogs", force: :cascade do |t|
t.string "title"
t.string "teaser"
t.text "body"
t.string "category", default: "General"
t.string "linked_module"
t.boolean "published", default: false
t.datetime "published_on"
t.integer "user_id"
t.integer "image_id"
t.integer "pdf_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug"
t.string "cta_read_more", default: "Read More"
t.string "cta_pdf", default: "Get My Free PDF"
t.string "cta_video", default: "Watch the Video"
t.string "convertkit_data_form_toggle"
t.string "convertkit_href"
t.integer "pin_image_id"
t.string "data_pin_description"
t.string "freebie_filename"
t.string "video_link"
t.string "freebie_type", default: "File"
t.string "freebie_description"
t.index ["image_id"], name: "index_blogs_on_image_id"
t.index ["pdf_id"], name: "index_blogs_on_pdf_id"
t.index ["pin_image_id"], name: "index_blogs_on_pin_image_id"
t.index ["slug"], name: "index_blogs_on_slug", unique: true
t.index ["user_id"], name: "index_blogs_on_user_id"
end
create_table "comments", force: :cascade do |t|
t.text "body"
t.integer "user_id"
t.integer "blog_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "approved", default: false
t.boolean "read", default: false
t.string "email"
t.string "name"
t.index ["blog_id"], name: "index_comments_on_blog_id"
t.index ["user_id"], name: "index_comments_on_user_id"
end