Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Postgres返回数组中的最后一项,而不是第一项_Ruby_Postgresql_Heroku_Ruby On Rails 4 - Fatal编程技术网

Ruby Postgres返回数组中的最后一项,而不是第一项

Ruby Postgres返回数组中的最后一项,而不是第一项,ruby,postgresql,heroku,ruby-on-rails-4,Ruby,Postgresql,Heroku,Ruby On Rails 4,我的控制器上有 @account = @user.accounts.first 其中我只想显示accounts数组中的第一项。但是,每当我添加一个新帐户时,@account现在引用accounts数组中的最后一项 在本地,使用SQLite3时,它可以正常工作,@account始终引用accounts数组中的第一项,但在使用postgresql的heroku上,则会发生这种情况 为什么会发生这种情况?根据rails存储库中的说明,您应该明确说明顺序,以确保其正常工作: @account = @u

我的控制器上有

@account = @user.accounts.first
其中我只想显示accounts数组中的第一项。但是,每当我添加一个新帐户时,@account现在引用accounts数组中的最后一项

在本地,使用SQLite3时,它可以正常工作,@account始终引用accounts数组中的第一项,但在使用postgresql的heroku上,则会发生这种情况

为什么会发生这种情况?

根据rails存储库中的说明,您应该明确说明顺序,以确保其正常工作:

@account = @user.accounts.order(:created_at).first

Postgres表没有隐式顺序。如果您想要有保证的订单,您需要在查询中添加
.order

@account = @user.accounts.order(id: :asc).first
将为您提供为此用户创建的第一个帐户

@account = @user.accounts.order(id: :desc).first

最后一顶。

漂亮的帽子……你有……)此外,这些关于Postgres订购的信息可能会有所帮助,我最终在这里发现了一个类似的问题