@Diesel-nick

Rails 5 has many through associations — почему возникает ошибка Could not find table 'team_users'?

Когда я создаю миграцию новой функцией в Rails 5 - create_join_table, и делаю ассоциацию has many through для двух таблиц users и teams, почему-то возникает ошибка Could not find table 'team_users'

Migration
rails g migration CreateJoinTableTeamUser team user


20161011185253_create_join_table_team_user.rb
class CreateJoinTableTeamUser < ActiveRecord::Migration[5.0]
  def change
    create_join_table :teams, :users do |t|
      t.index [:team_id, :user_id]
      t.index [:user_id, :team_id]
    end
  end
end


team.rb
class Team < ApplicationRecord
  has_many :team_user
  has_many :users, through: :team_user
end


user.rb
class User < ApplicationRecord
  has_many :team_user
  has_many :teams, through: :team_user
end


team_user.rb
class TeamUser < ApplicationRecord
  include Userstampable::Stampable

  belongs_to :user
  belongs_to :team

  # validates :user_id, presence: true
  # validates :team_id, presence: true
end


Terminal - ошибка
2.3.1 :001 > team = Team.first
...
2.3.1 :002 > me = User.first
...
2.3.1 :003 > team.users << me
...
ActiveRecord::StatementInvalid: Could not find table 'team_users'
  • Вопрос задан
  • 215 просмотров
Решения вопроса 1
lavandosovich
@lavandosovich
Доктор Болтологии
У тебя вот так:
class CreateJoinTableTeamUser < ActiveRecord::Migration[5.0]
  def change
    create_join_table :teams, :users do |t|
      t.index [:team_id, :user_id]
      t.index [:user_id, :team_id]
    end
  end
end


А надо:
class CreateTeamUser < ActiveRecord::Migration[5.0]
  def change
    create_table :team_users do |t|
      t.integer team_id
      t.integer user_id
    end
  end
end


Соответственно команда:
rails g migration CreateTeamUsers team_id:integer user_id:integer
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы