Althogh我想通过文章的平均评分来订购记录,当我选中ORDER BY created_at DESC
时会自动添加development.log
。
为什么附加此条件?我该如何删除它?
.schema文章
CREATE TABLE "articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "content" text(400), "user_id" integer, "created_at" datetime, "updated_at" datetime,"category_id" integer, "rating" real);
CREATE INDEX "index_articles_on_user_id_and_created_at" ON "articles" ("user_id", "created_at");
\模型\ category.rb
class Category < ActiveRecord::Base
has_many :articles
...
end
\模型\ article.rb
class Article < ActiveRecord::Base
belongs_to :user
belongs_to :category
...
end
\ controllers \ category_controller.rb
class CategoriesController < ApplicationController
def index
@articles = Article.select('*, AVG(rating) AS avg_rate').group(:category_id).order('avg_rate DESC')
end
...
end
\ LOG \ development.log 附加了ORDER BY created_at DESC 。
Processing by CategoriesController#index as HTML
[1m[36mArticle Load (1.0ms)[0m [1mSELECT *, AVG(rating) AS avg_rate FROM "articles" GROUP BY category_id ORDER BY created_at DESC, avg_rate DESC[0m
....
答案 0 :(得分:0)
如果您想要default_scope
,但不希望它仅针对特定查询,请在查询中使用unscoped
。例如:
Article.unscoped.select('*, AVG(rating) AS avg_rate').group(:category_id).order('avg_rate DESC')