简体   繁体   English

Rails 3/4 Rails Scaffold .update方法导致在Heroku上更改索引顺序

[英]Rails 3/4 Rails Scaffold .update method causes index order to change on Heroku

I built a webapp to test buying coffee. 我建立了一个网络应用程序来测试购买咖啡。

I used rails generate scaffold to make my item Vendor: 我使用导轨生成支架来制作我的物品供应商:

rails generate scaffold vendor expresso_count:integer cuppucino_count:integer 

It has a few other variables not included above, but they all of type integer 它还有上面未包含的其他一些变量,但它们都是整数类型

When a user purchases a coffee, a variable in vendor eg: cuppucino_count is updated. 用户购买咖啡时,将更新供应商中的变量,例如:cuppucino_count。 It uses this custom method in the Vendor controller which uses .update 它在使用.update的供应商控制器中使用此自定义方法

     def purchase_pass
@vendor = Vendor.find(params[:id])

# checks vendor pass
if @vendor.vendor_pass.to_s == vendor_params[:vendor_pass]
  flash[:success] = "Thank you for your purchase of 1x #{ params[:type] }."
  #updates coffee type
  if params[:type] == 'Espresso'
    coffee_count = @vendor.expresso_count
    coffee_count = coffee_count +1
    @vendor.update(expresso_count: coffee_count)
  end

  redirect_to purchase_thanks_path
else
  flash[:error] = "Pass incorrect. Please ensure you enter the correct pass."
  render :purchase
end

end 结束

Which is actioned by this link. 该链接对此起作用。

  <p><%= link_to vendor_purchase_path(id: @vendor.id, type: 'Americano') do %> <span style="font-size: 300%;" class="fa fa-coffee"></span>Americano <% end %></p>

On Heroku, the index.html view for vendors, the order that the vendors get displayed changes whenever a vendor gets updated using either the edit.html view or the .update method. 在Heroku上,供应商的index.html视图上,每当使用edit.html视图或.update方法更新供应商时,供应商显示的顺序都会更改。

ie: if there was 即:如果有

Vendor 1, Vendor 2, Vendor 3

It changes to 变成

Vendor 2, Vendor 3, Vendor 1 

if Vendor 1 was the one updated 如果供应商1是更新的

However the order doesn't change on my local installation. 但是,顺序在我的本地安装上不会更改。

Any ideas what is causing this? 任何想法是什么原因造成的?

Here is a link to my git repo: https://bitbucket.org/umlungu/mybeans 这是我的git仓库的链接: https : //bitbucket.org/umlungu/mybeans

What is relevant to the question is how the @vendors variable is being set in the index method; 与这个问题有关的是如何在index方法中设置@vendors变量; that is the one which will determine what is being displayed in the index page. 那将决定在索引页面中显示的内容。

If you have left the code from the scaffold as it is, then it will most likely look as follows: 如果您保留了脚手架上的代码,那么它很可能看起来如下:

def index
  @vendors = Vendor.all
end

This runs a select * from vendors query on the table. 这将在表上的select * from vendors查询中运行select * from vendors

Note that the query does not have any ordering clause. 请注意,查询没有任何排序子句。 From the postgres documentation for select : postgres文档中进行select

If the ORDER BY clause is specified, the returned rows are sorted in the specified order. 如果指定了ORDER BY子句,则返回的行将以指定的顺序排序。 If ORDER BY is not given, the rows are returned in whatever order the system finds fastest to produce. 如果未给出ORDER BY,则以系统认为最快的顺序返回行。 (See ORDER BY Clause below.) (请参阅下面的ORDER BY子句。)

So that explains why the order displayed is different locally and on heroku. 因此,这解释了为什么显示的顺序在本地和heroku上不同。

For making the results show up in a specific order, specify the order clause as follows: 为了使结果按特定顺序显示,请指定order子句,如下所示:

def index
  @vendors = Vendor.order(:id)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM