3

我在 heroku 上使用带有 PG 数据库的 activeRecord 订单功能时遇到问题。我有以下代码,目前工作得很好,除了偶尔记录不符合我想要的顺序的事实。所以,我尝试在数据库上使用 .order 函数,现在我得到了 PG::UndefinedColumn: ERROR。如果我删除 .order 功能,它会再次起作用。

当它中断时,模型或数据库中没有任何变化。它只是添加 .order 函数来尝试和控制数据库记录的顺序。

此外,它在 sqlite3 数据库上的开发中工作得很好。

这是有效的代码:

1 <table class="table table-striped table-bordered table-condensed table-hover">
2   <tr>
3     <td></td>
4     <% @pool.weeks.each do |week| %>
5       <td class="entry-header">
6         <%= "Week #{week.weekNumber}" %> 
7       </td>
8     <% end %>
9   </tr>

这是导致它中断的更改:

Change line 4 from this:
   4     <% @pool.weeks.each do |week| %>
to this:
   4     <% @pool.weeks.order("weekNumber ASC").each do |week| %>

一旦我添加了 .order 函数,它就会在 heroku 上中断并出现以下错误:

app[web.1]: ActionView::Template::Error (PG::UndefinedColumn: ERROR:  column "weeknumber" does not exist
app[web.1]:     2:   <tr>
app[web.1]: LINE 1: ...M "weeks"  WHERE "weeks"."pool_id" = $1  ORDER BY weekNumber...
app[web.1]: : SELECT "weeks".* FROM "weeks"  WHERE "weeks"."pool_id" = $1  ORDER BY weekNumber ASC):
app[web.1]:
app[web.1]:     1: <table class="table table-striped table-bordered table-condensed table-hover">
app[web.1]:     4:     <% @pool.weeks.order("weekNumber ASC").each do |week| %>
app[web.1]:     3:     <td></td>
app[web.1]: LINE 1: ...M "weeks"  WHERE "weeks"."pool_id" = $1  ORDER BY weekNumber...
app[web.1]:     5:       <td class="entry-header">
app[web.1]:   app/views/pools/_show_board.html.erb:4:in   `_app_views_pools__show_board_html_erb___2914673273129495301_70355170381800'
app[web.1]:
app[web.1]:     3:     <td></td>
app[web.1]:   app/views/pools/show.html.erb:28:in `_app_views_pools_show_html_erb__33868809623974782_70355169938300'
app[web.1]:     6:         <%= "Week #{week.weekNumber}" %>
app[web.1]: ActionView::Template::Error (PG::UndefinedColumn: ERROR:  column "weeknumber" does not exist
app[web.1]:
app[web.1]:     7:       </td>
app[web.1]: : SELECT "weeks".* FROM "weeks"  WHERE "weeks"."pool_id" = $1  ORDER BY weekNumber ASC):
app[web.1]:   app/views/pools/_show_board.html.erb:4:in `_app_views_pools__show_board_html_erb___2914673273129495301_70355170381800'
app[web.1]:     2:   <tr>
app[web.1]:     5:       <td class="entry-header">
app[web.1]:   app/views/pools/show.html.erb:28:in `_app_views_pools_show_html_erb__33868809623974782_70355169938300'
app[web.1]:     7:       </td>
app[web.1]:
app[web.1]:
app[web.1]:                                                              ^
app[web.1]:     1: <table class="table table-striped table-bordered table-condensed table-hover">
app[web.1]:     4:     <% @pool.weeks.order("weekNumber ASC").each do |week| %>
app[web.1]:     6:         <%= "Week #{week.weekNumber}" %>
app[web.1]:
heroku[router]: at=info method=GET path=/pools/1 host=fb-pools.herokuapp.com fwd="76.250.116.65" dyno=web.1 connect=5ms service=142ms status=500 bytes=643
heroku[web.1]: Stopping all processes with SIGTERM

数据库中没有任何变化。唯一的变化是添加了 .order 函数。我猜我的迁移中缺少一些东西,但我不确定是什么。

这是与周模型关联的迁移文件:

class CreateWeeks < ActiveRecord::Migration
  def change
    create_table :weeks do |t|
      t.integer :state
      t.integer :pool_id

      t.timestamps
    end
  end
end

class AddWeekNumberToWeeks < ActiveRecord::Migration
  def change
    add_column :weeks, :weekNumber, :integer
  end
end

以下是 Pool 和 Week 模型:

class Pool < ActiveRecord::Base

  POOL_TYPES = { PickEm: 0, PickEmSpread: 1, Survivor: 2, SUP: 3 }

  has_many :users, through: :pool_memberships, dependent: :destroy
  has_many :pool_memberships, dependent: :destroy
  has_many :weeks, dependent: :destroy
  has_many :entries, dependent: :destroy

  # Make sure protected fields aren't updated after a week has been created on the pool
  validate :checkUpdateFields, on: :update

  attr_accessor :password

  validates :name,     presence:   true,
                       length:      { :maximum => 30 },
                       uniqueness:  { :case_sensitive => false }
  validates :poolType, inclusion:   { in: 0..3 }
  validates :allowMulti, inclusion: { in: [true, false] }
  validates :isPublic, inclusion:   { in: [true, false] }

  ...   

end

class Week < ActiveRecord::Base

  STATES = { Pend: 0, Open: 1, Closed: 2, Final: 3 }

  belongs_to :pool
  has_many   :games, dependent: :destroy
  has_many   :picks, dependent: :destroy

  accepts_nested_attributes_for :games

  ...

end
4

2 回答 2

4

您的列名称为,weekNumber但 ActiveRecord 不区分大小写,因此正在寻找名为weeknumber.

将您的列名更改为week_number并将您的订单更新为week_number,您会没事的。

于 2013-10-03T19:02:33.450 回答
2

用双引号分隔 CamelCase 名称。

由此:

<% @pool.weeks.order("weekNumber ASC").each do |week| %>

对此

<% @pool.weeks.order("\"weekNumber\" ASC").each do |week| %>
于 2013-11-08T23:42:46.363 回答