迁移后为什么我的测试失败?

时间:2016-02-23 17:47:05

标签: ruby-on-rails ruby-on-rails-4

我想在玩家和比赛之间使用has_and_belongs_to_many协会。我已经创建了迁移,之后我的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 个答案:

答案 0 :(得分: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

&#34;关系&#34;在错误消息中表示&#34;表&#34;:

  

错误:关系&#34; matches_players&#34;不存在