2013-10-03 44 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| %> 
相關問題