Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails通过集合选择创建多个连接记录_Ruby On Rails_Nested Attributes_Has Many Through_Collection Select - Fatal编程技术网

Ruby on rails rails通过集合选择创建多个连接记录

Ruby on rails rails通过集合选择创建多个连接记录,ruby-on-rails,nested-attributes,has-many-through,collection-select,Ruby On Rails,Nested Attributes,Has Many Through,Collection Select,我对rails非常陌生,我尝试让三个模型与has\u many THOUN关系、联接模型上的备用类名以及向联接模型添加记录的集合选择表单一起工作。下面是三个模型。我已为所有关联接受\u嵌套的\u属性\u 用户模型 has_many :match_opponents has_many :home_opponents, :class_name => 'MatchOpponent', :foreign_key => 'home_opponent_id' has_many :away_opp

我对rails非常陌生,我尝试让三个模型与has\u many THOUN关系、联接模型上的备用类名以及向联接模型添加记录的集合选择表单一起工作。下面是三个模型。我已为所有关联接受\u嵌套的\u属性\u

用户模型

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 :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, :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
has_many :match_opponents
has_many :home_opponents, :through => :match_opponents
has_many :away_opponents, :through => :match_opponents
对手模型

belongs_to :user, :inverse_of => :match_opponents
belongs_to :match, :inverse_of => :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
我的控制器用于新建和创建:

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]
我尝试了很多不同的模型配置,但是在记录了match_id和user_id的情况下,无法在join model matchhatcher上保存多条记录


提前感谢您的帮助

首先,您需要了解更多有关控制器操作的信息。 你可以在报纸上读到他们

例如:

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

谢谢!用以下方法解决了这个问题。需要在用户模型而不是联接模型上创建备用类:

用户模型

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 :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, :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
has_many :match_opponents
has_many :home_opponents, :through => :match_opponents
has_many :away_opponents, :through => :match_opponents
对手模型

belongs_to :user, :inverse_of => :match_opponents
belongs_to :match, :inverse_of => :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
以上所有属性都有接受的嵌套属性

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

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}