gpt4 book ai didi

mysql - Rails4 - 不明确的列名和没有这样的列错误

转载 作者:行者123 更新时间:2023-11-29 01:40:00 24 4
gpt4 key购买 nike

我的 Controller 中有以下内容来列出用户。

@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

请帮助解决此错误。

最佳答案

您可能在加入 user_languagesUser 上有一些默认范围,并且在这两个表中都有 created_at 列。所以你应该指出你想要订购哪个表的created_at:

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

关于mysql - Rails4 - 不明确的列名和没有这样的列错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27128846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com