automatch 시스템을 만들려고하지만 아래 코드는 "matches"테이블에만 삽입합니다. matches_users 조인 테이블에 두 플레이어 ID를 모두 삽입하고 싶습니다.이를 수정하는 방법은 무엇입니까? 감사! 클래스의 여러 인스턴스에 대한 조인 테이블 업데이트
class Match < ActiveRecord::Base
# Associations
has_and_belongs_to_many :users
belongs_to :user
def initialize_match
return false unless empty?
players = match_players
with_lock do
# Makes transaction
self.user_1 = players[0].id
self.user_2 = players[1].id
finished = false
save!
end
end
def match_players
users = User.limit(2).order("RANDOM()")
end
그리고 내 사용자 클래스
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Associations
has_and_belongs_to_many :matches
는 당신이'belongs_to 제거 할 필요가 있다고 생각 :이 – emaillenin