我在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
答案 0 :(得分:4)
您的列名称为weekNumber
,但ActiveRecord不区分区分大小写,因此正在查找名为weeknumber
的列。
将您的列名更改为week_number
并将订单更新为week_number
,您就可以了。
答案 1 :(得分:2)
使用双引号分隔CamelCase名称。
从此:
<% @pool.weeks.order("weekNumber ASC").each do |week| %>
到这个
<% @pool.weeks.order("\"weekNumber\" ASC").each do |week| %>