Rails4:为什么' created_at DESC'自动附加?我该如何删除它?

时间:2015-02-28 22:19:39

标签: sql ruby-on-rails ruby ruby-on-rails-4

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
....

1 个答案:

答案 0 :(得分:0)

如果您想要default_scope,但不希望它仅针对特定查询,请在查询中使用unscoped。例如:

Article.unscoped.select('*, AVG(rating) AS avg_rate').group(:category_id).order('avg_rate DESC')