2016-02-23 73 views
0

我想使用has_and_belongs_to_many Association,在球員和比賽之間。我創建了遷移,之後我的rspec測試失敗了。重新啓動數據庫沒有幫助。爲什麼遷移後我的測試失敗?

錯誤消息:

Failures: 

    1) PlayersController user is signed in DELETE destroy current user is admin deletes the player 
    Failure/Error: @player = Player.find(params[:id]).destroy 

    ActiveRecord::StatementInvalid: 
     PG::UndefinedTable: ERROR: relation "matches_players" does not exist 
     LINE 5:    WHERE a.attrelid = '"matches_players"'::regcl... 
               ^
     :    SELECT a.attname, format_type(a.atttypid, a.atttypmod), 
          pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod 
         FROM pg_attribute a LEFT JOIN pg_attrdef d 
         ON a.attrelid = d.adrelid AND a.attnum = d.adnum 
         WHERE a.attrelid = '"matches_players"'::regclass 
         AND a.attnum > 0 AND NOT a.attisdropped 
         ORDER BY a.attnum 
    # ./app/controllers/players_controller.rb:57:in `destroy' 
    # /home/adam/.rvm/gems/ruby-2.2.2/gems/devise-3.5.3/lib/devise/test_helpers.rb:19:in `block in process' 
    # /home/adam/.rvm/gems/ruby-2.2.2/gems/devise-3.5.3/lib/devise/test_helpers.rb:72:in `catch' 
    # /home/adam/.rvm/gems/ruby-2.2.2/gems/devise-3.5.3/lib/devise/test_helpers.rb:72:in `_catch_warden' 
    # /home/adam/.rvm/gems/ruby-2.2.2/gems/devise-3.5.3/lib/devise/test_helpers.rb:19:in `process' 
    # ./spec/controllers/players_controller_spec.rb:208:in `block (6 levels) in <top (required)>' 
    # ./spec/controllers/players_controller_spec.rb:207:in `block (5 levels) in <top (required)>' 
    # ------------------ 
    # --- Caused by: --- 
    # PG::UndefinedTable: 
    # ERROR: relation "matches_players" does not exist 
    # LINE 5:    WHERE a.attrelid = '"matches_players"'::regcl... 
    #           ^
    # ./app/controllers/players_controller.rb:57:in `destroy' 

    2) PlayersController user is signed in DELETE destroy current user is admin redirects to tournament 
    Failure/Error: @player = Player.find(params[:id]).destroy 

    ActiveRecord::StatementInvalid: 
     PG::UndefinedTable: ERROR: relation "matches_players" does not exist 
     LINE 5:    WHERE a.attrelid = '"matches_players"'::regcl... 
               ^
     :    SELECT a.attname, format_type(a.atttypid, a.atttypmod), 
          pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod 
         FROM pg_attribute a LEFT JOIN pg_attrdef d 
         ON a.attrelid = d.adrelid AND a.attnum = d.adnum 
         WHERE a.attrelid = '"matches_players"'::regclass 
         AND a.attnum > 0 AND NOT a.attisdropped 
         ORDER BY a.attnum 
    # ./app/controllers/players_controller.rb:57:in `destroy' 
    # /home/adam/.rvm/gems/ruby-2.2.2/gems/devise-3.5.3/lib/devise/test_helpers.rb:19:in `block in process' 
    # /home/adam/.rvm/gems/ruby-2.2.2/gems/devise-3.5.3/lib/devise/test_helpers.rb:72:in `catch' 
    # /home/adam/.rvm/gems/ruby-2.2.2/gems/devise-3.5.3/lib/devise/test_helpers.rb:72:in `_catch_warden' 
    # /home/adam/.rvm/gems/ruby-2.2.2/gems/devise-3.5.3/lib/devise/test_helpers.rb:19:in `process' 
    # ./spec/controllers/players_controller_spec.rb:213:in `block (5 levels) in <top (required)>' 
    # ------------------ 
    # --- Caused by: --- 
    # PG::UndefinedTable: 
    # ERROR: relation "matches_players" does not exist 
    # LINE 5:    WHERE a.attrelid = '"matches_players"'::regcl... 
    #           ^
    # ./app/controllers/players_controller.rb:57:in `destroy' 

遷移:

class MatchesPlayers < ActiveRecord::Migration 
    def change 
    create_table :table_matches_players, id: false do |t| 
     t.belongs_to :match, index: true 
     t.belongs_to :player, index: true 
    end 
    end 
end 

播放器型號:

class Player < ActiveRecord::Base 

    belongs_to :user 
    belongs_to :tournament 
    has_and_belongs_to_many :matches, through: :matches_players 
end 

比賽模式:

class Match < ActiveRecord::Base 

    belongs_to :tournament 
    belongs_to :round 
    has_and_belongs_to_many :players, through: :matches_players 
end 

模式文件:

create_table "table_matches_players", id: false, force: :cascade do |t| 
    t.integer "match_id" 
    t.integer "player_id" 
    end 

PlayersController破壞行動:

def destroy 
    if current_user == @admin 
     @player = Player.find(params[:id]).destroy 
     flash[:success] = "Player deleted" 
     redirect_to @tournament 
    else 
     redirect_to @tournament 
    end 
    end 
+1

我相信,在您的遷移,此行應該是'CREATE_TABLE:matches_players',而不是': table_matches_players' ... –

回答

1

你叫你的表:table_matches_players。它應該只是:matches_players

class MatchesPlayers < ActiveRecord::Migration 
    def change 
    create_table :matches_players, id: false do |t| 
     t.belongs_to :match, index: true 
     t.belongs_to :player, index: true 
    end 
    end 
end 
錯誤消息

「關係」 的意思是 「表」:

ERROR: relation "matches_players" does not exist

+0

你說得對。我不知道爲什麼詞「表」是在我的遷移。我的創建遷移的命令如下所示。 「rails g遷移MatchesPlayers」。 – Nekron