rails has_many 通过使用 collection_select 创建多个连接记录

rails has_many through create multiple join records with collection_select

我是 rails 的新手,正在尝试让三个模型与 has_many 通过关系一起工作,连接模型上的备用 class 名称,以及 collection_select 表单将记录添加到连接模型。三种型号如下。我有 "accepts_nested_attributes_for" 所有协会。

用户模型

has_many :match_opponents
has_many :home_opponents, :class_name => 'MatchOpponent', :foreign_key => 'home_opponent_id'
has_many :away_opponents, :class_name => 'MatchOpponent', :foreign_key => 'away_opponent_id'
has_many :matches, :through => :match_opponents

匹配模型

has_many :match_opponents
has_many :home_opponents, :class_name => 'MatchOpponent', :foreign_key => 'home_opponent_id'
has_many :away_opponents, :class_name => 'MatchOpponent', :foreign_key => 'away_opponent_id'
has_many :users, :through => :match_opponents

匹配对手模型

belongs_to :user, :inverse_of => :match_opponents
belongs_to :match, :inverse_of => :match_opponents

用于新建和创建的我的匹配控制器:

def new
  @match = Match.new
  @match.home_opponents.build
  @match.away_opponents.build
end

def create
  @match = Match.create(match_params)
  @home_opponents = @match.home_opponents.create!(params[:match_opponents])
  @away_opponents = @match.away_opponents.create!(params[:match_opponents])
  if @match.save 
    redirect_to @match, notice: 'Match was successfully created.'
  end
end

我的表格:

= simple_form_for @match, html: { multipart: true } do |f|
  = simple_fields_for :home_opponents do |ff|
    = ff.collection_select :user_id, User.all, :id, :name, {}, {multiple: true}

  = simple_fields_for :away_opponents do |ff|
    = ff.collection_select :user_id, User.all, :id, :name, {}, {multiple: true}

每个模型的相关允许参数:

def match_params
  params.require(:match).permit(Match::MATCH_ATTRIBUTES)
end

MATCH_ATTRIBUTES = [:match_opponent_ids => [], :user_ids => [], match_opponents_attributes: MatchOpponent::MATCH_OPPONENT_ATTRIBUTES]

def user_params
  params.require(:user).permit(User::USER_ATTRIBUTES)
end

USER_ATTRIBUTES = [:match_opponent_ids => [], :match_ids => [], :match_opponents_attributes: MatchOpponent::MATCH_OPPONENT_ATTRIBUTES]

def match_opponent_params
  params.require(:match_opponent).permit(MatchOpponent::MATCH_OPPONENT_ATTRIBUTES)
end

MATCH_OPPONENT_ATTRIBUTES = [:id, :home_opponent_id, :away_opponent_id, :match_id, :user_id]

我已经尝试了很多不同的模型配置,但无法在连接模型 (MatchOpponent) 上获取多条记录以同时保存 match_id 和 user_id 记录。

在此先感谢您的帮助!

首先,您需要了解更多有关控制器操作的信息。 您可以在 Rails Guides

中阅读有关它们的信息

例如:

def create
  @match = Match.create(match_params)
  @home_opponents = @match.home_opponents.create!(params[:match_opponents])
  @away_opponents = @match.away_opponents.create!(params[:match_opponents])
  if @match.save 
    redirect_to @match, notice: 'Match was successfully created.'
  end
end

Match.create 已经保存了对象,您可以使用 if @match.save 再次保存该对象。这应该看起来像:

def create
  @match = Match.new(match_params)
  if @match.save 
    #create opponents here
    redirect_to @match, notice: 'Match was successfully created.'
  end
end

谢谢!用以下方法解决了这个问题。需要在用户模型而不是连接模型(MatchOpponent)上制作备用 类:

用户模型

has_many :match_opponents
has_many :matches, :through => :match_opponents
has_many :home_opponents, :through => :match_opponents
has_many :away_opponents, :through => :match_opponents

匹配模型

has_many :match_opponents
has_many :home_opponents, :through => :match_opponents
has_many :away_opponents, :through => :match_opponents

匹配对手模型

belongs_to :home_opponent, :class_name => 'User', :foreign_key => 'home_opponent_id'
belongs_to :away_opponent, :class_name => 'User', :foreign_key => 'away_opponent_id'
belongs_to :match, :inverse_of => :match_opponents

以上都有accepts_nested_attributes_for.

用于新建和创建的我的匹配控制器:

def new
  @match = Match.new
end

def create
  @match = Match.new(match_params)
  @match.update!(:status => 'pending')
  if @match.save 
    @match.match_opponents.where.not(:home_opponent_id => 'nil').each do |o| o.update!(:user_id => o.home_opponent_id) end
    @match.match_opponents.where.not(:away_opponent_id => 'nil').each do |o| o.update!(:user_id => o.away_opponent_id) end
   redirect_to @match, notice: 'Match was successfully created.'
  end
end

我的表格:

= f.collection_select :home_opponent_ids, User.order(:first_name), :id, :name, {}, {multiple: true}
= f.collection_select :away_opponent_ids, User.order(:first_name), :id, :name, {}, {multiple: true}