0

このメソッドをherokuで機能させるのに問題があります。ページを読み込もうとすると、上記のエラーメッセージが表示されます。以前の投稿で助けがありましたが、リファクタリングが機能していないようです。私が読んだことから、モデルのすべての列を検索とグループに含める必要があります..それは正しいですか?メソッドとスキーマは次のとおりです

方法

def self.popular_recipes
  select('recipes.*, count(*) AS dish_count').
  group('dish_name').
  order('dish_count DESC')
 end

スキーマ

create_table "recipes", :force => true do |t|
t.string   "dish_name"
t.string   "difficulty"
t.text     "preperation_time"
t.datetime "created_at",          :null => false
t.datetime "updated_at",          :null => false
t.integer  "user_id"
t.string   "avatar_file_name"
t.string   "avatar_content_type"
t.integer  "avatar_file_size"
t.datetime "avatar_updated_at"
t.integer  "country_id"
t.string   "category"
t.text     "description"
end
4

1 に答える 1

1

recipes.*問題は、 PostgreSQLを混乱させるものを選択していることです。

recipes500レコードのテーブルに10個の一意のdish_nameがあるとします。句を指定するGROUP BY dish_nameと、10行のみが返されます。では、を要求した場合recipes.*、Postgresは10行の他の列に入力する方法をどのように知ることになっていますか?

代わりにこれを行ってください:

def self.popular_recipes
  select('dish_name, count(*) AS dish_count').
  group('dish_name').
  order('dish_count DESC')
 end

MySQLはこの状況では動作が異なり(エラーなし)、ごとdish_nameに1行返されますが、どの行になるかは基本的に未定義であることに注意してください。

于 2012-12-15T02:42:05.277 に答える