2014-11-25 74 views
2

我在我的控制器列出用戶以下。Rails4 - 模糊的列名稱,並沒有這樣的列錯誤

@users = User.paginate(:page => params[:page]) 

我試着用以下

@users = User.paginate(:page => params[:page]).order('created_at DESC') 

我有以下錯誤

ActiveRecord::StatementInvalid in UsersController#index 
SQLite3::SQLException: ambiguous column name: created_at: SELECT DISTINCT "users"."id" FROM "users" LEFT OUTER JOIN "user_languages" ON "user_languages"."user_id" = "users"."id" WHERE (user_languages.level_id = 1) ORDER BY created_at DESC LIMIT 30 OFFSET 0 

後來我改了行如下更改順序

@users = User.paginate(:page => params[:page]).order('user.created_at DESC') 

和我正在獲得以下成果翼錯誤。

ActiveRecord::StatementInvalid in UsersController#index 
SQLite3::SQLException: no such column: user.created_at: SELECT DISTINCT "users"."id" FROM "users" LEFT OUTER JOIN "user_languages" ON "user_languages"."user_id" = "users"."id" WHERE (user_languages.level_id = 1) ORDER BY user.created_at DESC LIMIT 30 OFFSET 0 

請幫助解決此錯誤。

+3

你有錯字:用戶計劃** S ** created_at – pozs 2014-11-25 14:10:44

回答

4

您可能在User上加入了一些默認範圍user_languages,並且在這兩個表中都有created_at列。所以,你應該指出,在其表的created_at您想訂購:

@user = User.paginate(page: params[:page]).order('users.created_at DESC')