2013-10-03 41 views
3

我在使用heroku上的PG数据库使用activeRecord命令功能时出现问题。现在只是很好,除了偶尔记录不符合我想要的顺序。所以,我尝试使用数据库上的.order函数,现在我得到了PG :: UndefinedColumn:ERROR。如果我删除了。为了功能,它再次工作。无论是在车型ActionView :: Template :: Error(PG :: UndefinedColumn:错误:列“weeknumber”不存在

没有任何改变。或者当它打破了数据库,这只是增加了.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 

这里有游泳池和周型号:

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

可能重复[Rails的报告不能找到一个列有(http://stackoverflow.com/questions/10628917/rails-reports-can-find-a-column-that-is-there) –

+0

@ZachKemp:这就是问题所在,他们不得不说'.order('“weekNumber”ASC')'带有列这些名字。或者将该列创建为':week_number',以便无人担心。 –

+0

它看起来像我有同样的问题。但是,该问题的解决方案是重做所有数据库并删除混合大小写表项。不幸的是,由于应用程序正在运行,因此我现在无法重做我的表格。那么,我怎样才能让order()函数处理这个混合大小写问题呢? –

回答

4

你有你的列名作为weekNumber但ActiveRecord的不尊重区分大小写,因此正在寻找名为weeknumber的列。

将您的专栏名称更改为week_number并将您的订单更新为week_number,您就会安然无恙。

2

用双引号分隔CamelCase名称。

从本:

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

TO这

<% @pool.weeks.order("\"weekNumber\" ASC").each do |week| %> 
相关问题